[R] Help with reshape/reShape and indexing
Uwe Ligges
ligges at statistik.tu-dortmund.de
Wed May 13 11:14:33 CEST 2009
Dana Sevak wrote:
> Dear R Helpers,
>
> I have trouble applying reShape and reshape although I read the documentation and several posts, so I would very much appreciate your help on the two points below.
>
> I have a dataframe
>
> df = data.frame(Name=c("a", "a", "a", "b", "b", "c"), X1=c("12", "13", "14", "20", "25", "30"), X2 = c(200, 250, 300, 600, 700, 4))
>
>> df
> Name X1 X2
> 1 a 12 200
> 2 a 13 250
> 3 a 14 300
> 4 b 20 600
> 5 b 25 700
> 6 c 30 900
>
> First I need to add an additional column to this dataframe that will count the number of rows per each Name entry. The resulting df should look like:
>
> df.index = data.frame(Name=c("a", "a", "a", "b", "b", "c"), X1=c(12, 13, 14, 20, 25, 30), X2 = c(200, 250, 300, 600, 700, 4), Index=c(1,2,3,1,2,1))
>
>> df.index
> Name X1 X2 Index
> 1 a 12 200 1
> 2 a 13 250 2
> 3 a 14 300 3
> 4 b 20 600 1
> 5 b 25 700 2
> 6 c 30 900 1
>
> How can I do this?
>
>
> Secondly, I would like to reshape this dataframe in the form:
>
>> df2
> 1 2 3
> a 12 13 14
> b 20 25 NA
> c 30 NA NA
This does it more or less your way:
ds <- split(df, df$Name)
ds <- lapply(ds, function(x){x$Index <- seq_along(x[,1]); x})
df2 <- unsplit(ds, df$Name)
tapply(df2$X1, df2[,c("Name", "Index")], function(x) x)
athough there may exist much easier ways ...
Uwe Ligges
> Since the df is sorted by Name and X2, I would need that the available X1 values populate the resulting rows in df2 from left to right (i.e. if only one value is available, it is written in the first column and the remaining columns get NAs). If I could generate the Index column, I think I could accomplish this with:
>
> df2 = reShape(df.index$X1, id=df.index$Name, colvar=df.index$Index)
> colnames(df2) = c("V1", "V2", "V3")
>
> However, is there a way to get to df2 without using the Index column and still have the NAs written as described above?
>
> Thank you so much for your help on these two issues.
>
> With best regards,
> Dana Sevak
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list