[R] how to add new rows in a dataframe?

arun smartpink111 at yahoo.com
Tue Apr 30 15:29:53 CEST 2013


Hi,
Not sure if this is what you meant.
dat1<- read.table(text="
id                t                     scores  scores2  scores3
2                 0                        1.2     1.4    1.9
2                 2                         2.3     2.5   2.2
2                 3                        3.6      3.2   3.4
2                 4                        5.6      5.9   5.7
2                 6                        7.8      7.5   7.6
3                 0                        1.6      1.8   1.9
3                 1                         1.2     1.7   1.5
3                 4                         1.5     1.9   1.6
",sep="",header=TRUE)
library(plyr)
 dat2<- ddply(dat1,.(id),summarize, t=seq(min(t),max(t)))
library(zoo)
 res<-na.locf(join(dat2,dat1,type="full"))
res
#   id t scores scores2 scores3
#1   2 0    1.2     1.4     1.9
#2   2 1    1.2     1.4     1.9
#3   2 2    2.3     2.5     2.2
#4   2 3    3.6     3.2     3.4
#5   2 4    5.6     5.9     5.7
#6   2 5    5.6     5.9     5.7
#7   2 6    7.8     7.5     7.6
#8   3 0    1.6     1.8     1.9
#9   3 1    1.2     1.7     1.5
#10  3 2    1.2     1.7     1.5
#11  3 3    1.2     1.7     1.5
#12  3 4    1.5     1.9     1.6
A.K.






________________________________
From: GUANGUAN LUO <guanguanluo at gmail.com>
To: arun <smartpink111 at yahoo.com> 
Sent: Tuesday, April 30, 2013 9:17 AM
Subject: Re: how to add new rows in a dataframe?



I would replace the rows but not only the variable "scores". In reality , i have 105 variables in each row .
thank you so much.

GG


2013/4/29 arun <smartpink111 at yahoo.com>

Hi,
>dat1<- read.table(text="
>id                t                     scores
>2                 0                        1.2
>2                 2                         2.3
>2                 3                        3.6
>2                 4                        5.6
>2                 6                        7.8
>3                 0                        1.6
>3                 1                         1.2
>3                 4                         1.5
>",sep="",header=TRUE)
>library(zoo)
>res1<-do.call(rbind,lapply(split(dat1,dat1$id),function(x) {t1<-seq(min(x$t),max(x$t));scores1<-na.locf(x$scores[match(t1,x$t)]);data.frame(id=rep(unique(x$id),length(t1)),t1,scores1)}))
> row.names(res1)<- 1:nrow(res1)
>
> res1
>#   id t1 scores1
>#1   2  0     1.2
>#2   2  1     1.2
>#3   2  2     2.3
>#4   2  3     3.6
>#5   2  4     5.6
>#6   2  5     5.6
>#7   2  6     7.8
>#8   3  0     1.6
>#9   3  1     1.2
>#10  3  2     1.2
>#11  3  3     1.2
>#12  3  4     1.5
>libray(plyr)
> dat2<-ddply(dat1,.(id),summarize,t=seq(min(t),max(t)))
>res2<-mutate(join(dat2,dat1,type="full"),scores=na.locf(scores))
>identical(res1,res2)
>#[1] TRUE
> res2
>#   id t scores
>#1   2 0    1.2
>#2   2 1    1.2
>#3   2 2    2.3
>#4   2 3    3.6
>#5   2 4    5.6
>#6   2 5    5.6
>#7   2 6    7.8
>#8   3 0    1.6
>#9   3 1    1.2
>#10  3 2    1.2
>#11  3 3    1.2
>#12  3 4    1.5
>
>A.K.
>
>
>>Hello , dear  experts,
>>I have my data like this:
>>
>>id                t                     scores
>>2                 0                        1.2
>>2                 2                         2.3
>>2                 3                        3.6
>>2                 4                        5.6
>>2                 6                        7.8
>>3                 0                        1.6
>>3                 1                         1.2
>>3                 4                         1.5
>>
>>I want to fullifill the "t", so i want to add the rows with the data of (t-1)
>>
>>just get another dataframe like this:
>>
>>id              t                 scores
>>2               0                 1.2
>>2               1                  1.2
>>2                2                         2.3
>>2                3                        3.6
>>2               4                        5.6            
>>2               5                         5.6
>>2                6                         7.8
>>3                 0                        1.6
>>3                 1                         1.2
>>3                2                         1.2
>>3                4                        1.5
>>
>>How can i get the result like this? In reality, i have 4000 obervations, so it's difficult to add the lines manuelly.
>>
>>Thank you so much.
>



More information about the R-help mailing list