[R] Odp: adding rows to table

Petr PIKAL petr.pikal at precheza.cz
Thu Sep 18 08:15:45 CEST 2008


Hi

r-help-bounces at r-project.org napsal dne 17.09.2008 11:08:29:

> Greetings everyone,
> 
> I'm trying to add a specific table or a specific number of rows (e.g.44) 
to a 
> table with no success.
> 
> This is my basic table
> > head(dataA)
>   year    plot spp       prop.B        DCA1    DCA2    DCA3    DCA4
> 1 2000    1   a1      0.031079  -0.0776 -0.0009  0.0259 -0.0457
> 2 2000    1   a2      0.968921  -0.0448  0.1479 -0.1343  0.1670
> 3 2000    2   a1      0.029218  -0.0776 -0.0009  0.0259 -0.0457
> 4 2000    2   a4      0.021678  -0.3052 -0.0275 -0.0330 -0.0516
> 5 2000    2   a5      0.088596   0.0357 -0.0382  0.0171 -0.0471
> 6 2000    2   a6      0.065033   0.1219 -0.0588 -0.1119 -0.1795
> 
> I want, every time that the plot number changes, to add a specific table 

> (data2) or a specific number of rows underneath.
> 
> I've tried several variations of this with no result
> 
> for (i in 2:nrow(dataA))
> {
> 
> 
ifelse(dataA$plot[i]!=dataA$plot[i-1],tab=data.frame(res[1:i,1:3]),a=1+1)
> 
> 
> rbind(as.matrix(dataA[1:i,]),as.matrix(dataB[,])),a=1+1) 
> 
> 
> }

One possibility is to use split lapply do.call approach.

add.row<-rep(NA,8*44)
dim(add.row)<-c(44,8)
names(add.row)
dim(add.row)
add.row<-data.frame(add.row)
names(add.row)<-names(data)
lapply(split(data, data$plot), function(x) rbind(x,add.row))

or if you want it in data frame

do.call(rbind,lapply(split(data, data$plot), function(x) 
rbind(x,add.row)))

Regards
Petr

> 
> 
> many thanks
> 
> ______________________________________________
> 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