Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

This is what I have:

id<-c(1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2)

measure<-c("speed","weight","time","speed","weight","time","speed","weight","time",

           "speed","weight","time","speed","weight","time","speed","weight","time")

value<-c(1.23,10.3,33,1.44,10.4,31,1.21,10.1,33,4.25,12.5,38,1.74,10.8,31,3.21,10.3,33)

testdf<-data.frame(id,measure,value) 

This is what I want:

id<-c(1,1,1,2,2,2)  

speed<-c(1.23,1.44,1.21,4.25,1.74,3.21)

weight<-c(10.3,10.4,10.1,12.5,10.8,10.3)

time<-c(33,31,33,37,31,33)

res<-data.frame(id,speed,weight,time) 

The issue lies in that my variables speed weight and time are repeated. I can get it done with a for loop with if statements but its a major headache and not very efficient. This is my first post on stackoverflow ... long time user first time question ... thanks yall!

1 Answer

0 votes
by (41.4k points)

Here, use rowid from data.table 

library(reshape2)

 

testdf$r <- data.table::rowid(testdf$measure); 

dcast(testdf, id + r ~ measure)

 

  id r speed time weight

1  1 1  1.23   33   10.3

2  1 2  1.44   31   10.4

3  1 3  1.21   33   10.1

4  2 4  4.25   38   12.5

5  2 5  1.74   31   10.8

6  2 6  3.21   33   10.3

1.In one line 

dcast(testdf, id + data.table::rowid(measure) ~ measure).

 

2.Without data.table

 testdf$r <- ave(testdf$id, testdf$meas, FUN = seq_along).

3. For learning the data.table package, refer to the below code:

library(data.table)

setDT(testdf)

testdf[, r := rowid(measure)]

dcast(testdf, id + r ~ measure)

If you wish to learn more about how to use python for data science, then go through this data science python course by Intellipaat for more insights.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...