Hello, I need to insert a line after every eigth row (group/suset) which should contains the following: <(an incremented ID),0,1,1,1,1>
I have two problems with constructing my code: 1. I am getting NAs in three of the columns and 2) I can not find a way to write the ID (group name) at the beginning of the inserted line. Below the # sign I show one of the ways I tried from a variation of a script I found searching the r list-help page. This is a small example of my data set. hcmid1[1:24,] ID dose Avep1 Avep2 Avep3 Avep4 1 50676 A 1 10 76.6030534351145 70.9993322946806 72.3357981731187 83.0880635365569 2 50676 A 1 50 12.0801526717557 7.6118406409971 7.69899956502827 17.7762205092268 3 50676 A 1 75 1.35496183206107 1.13788114845315 2.34884732492388 4.97547302032236 4 50676 A 1 100 0.598759541984733 0.567549521477854 0.622553284036538 2.03807521607101 5 50676 A 1 120 0.65601145038168 0.0584242154462498 1.40550239234450 0.908082223779492 6 50676 A 1 140 0.846851145038168 0.303249499221010 0.266420182688125 1.86580238262088 7 50676 A 1 160 0.372137404580153 1.00990429557089 0.492061765985211 2.42641906096706 8 50676 A 1 200 0.319656488549619 0.614845314934342 0.58721183123097 1.07159542163046 11 50676 A 2 10 84.1897233201581 85.3687739463602 87.1918801353311 90.1436464088398 12 50676 A 2 50 18.4106511337633 17.0019157088123 16.2155630739488 24.6408839779006 13 50676 A 2 75 1.91387559808613 1.96360153256705 1.15998066698888 2.43093922651934 14 50676 A 2 100 0.434262533804868 1.38888888888889 0.700821652972451 1.13812154696133 15 50676 A 2 120 0.309444560016643 0.58668582375479 0.640405993233447 1.30386740331492 16 50676 A 2 140 0.306844185562721 0.562739463601533 0.447075882068633 0.674033149171271 17 50676 A 2 160 0.260037445392137 0.311302681992337 0.809569840502659 0.773480662983426 18 50676 A 2 200 0.473268150613689 0.454980842911878 0.749154180763654 0.895027624309393 21 50676 A 3 10 88.8305225324855 74.5594713656388 83.3893050891243 81.4887848163668 22 50676 A 3 50 14.5700857063865 16.5198237885463 20.5114957375355 22.8493961054967 23 50676 A 3 75 0.940005529444292 2.17511013215859 2.9708085765952 1.72541286665024 24 50676 A 3 100 0.708460049764999 0.85352422907489 0.800826659777836 0.665516391422234 25 50676 A 3 120 0.63588609344761 0.49559471365639 0.561870317747353 0.391298989401036 26 50676 A 3 140 0.311031241360244 0.385462555066082 0.213123223973134 0.289622874044861 27 50676 A 3 160 0.228089576997513 0.426762114537447 0.180831826401447 0.511461671185606 28 50676 A 3 200 0.124412496544098 0.357929515418503 0.0129165590286748 0.406704461424698 . . . This is the script I used to obtain the output below. ----------------------------------------------- .row <- rep(1,6*1) dim(.row)<-c(1,6) .row <- data.frame(.row) names(.row) <- names(hcmid1) .row$ID <- hcmid1$ID[1] .row$dose <- 1.0 .row$Avep1 <- 0.0 .row$Avep2 <- 0.0 .row$Avep3 <- 0.0 .row$Avep4 <- 0.0 do.call(rbind,lapply(split(hcmid1, hcmid1$ID), function(x) rbind(x,.row))) ID dose Avep1 Avep2 Avep3 Avep4 50676 A 1 .1 50676 A 1 10 76.6030534351145 70.9993322946806 72.3357981731187 83.0880635365569 50676 A 1 .2 50676 A 1 50 12.0801526717557 7.6118406409971 7.69899956502827 17.7762205092268 50676 A 1 .3 50676 A 1 75 1.35496183206107 1.13788114845315 2.34884732492388 4.97547302032236 50676 A 1 .4 50676 A 1 100 0.598759541984733 0.567549521477854 0.622553284036538 2.03807521607101 50676 A 1 .5 50676 A 1 120 0.65601145038168 0.0584242154462498 1.40550239234450 0.908082223779492 50676 A 1 .6 50676 A 1 140 0.846851145038168 0.303249499221010 0.266420182688125 1.86580238262088 50676 A 1 .7 50676 A 1 160 0.372137404580153 1.00990429557089 0.492061765985211 2.42641906096706 50676 A 1 .8 50676 A 1 200 0.319656488549619 0.614845314934342 0.58721183123097 1.07159542163046 50676 A 1 .9 50676 A 1 <NA> 0 <NA> <NA> 0 50676 A 2 .11 50676 A 2 10 84.1897233201581 85.3687739463602 87.1918801353311 90.1436464088398 50676 A 2 .12 50676 A 2 50 18.4106511337633 17.0019157088123 16.2155630739488 24.6408839779006 50676 A 2 .13 50676 A 2 75 1.91387559808613 1.96360153256705 1.15998066698888 2.43093922651934 50676 A 2 .14 50676 A 2 100 0.434262533804868 1.38888888888889 0.700821652972451 1.13812154696133 50676 A 2 .15 50676 A 2 120 0.309444560016643 0.58668582375479 0.640405993233447 1.30386740331492 50676 A 2 .16 50676 A 2 140 0.306844185562721 0.562739463601533 0.447075882068633 0.674033149171271 50676 A 2 .17 50676 A 2 160 0.260037445392137 0.311302681992337 0.809569840502659 0.773480662983426 50676 A 2 .18 50676 A 2 200 0.473268150613689 0.454980842911878 0.749154180763654 0.895027624309393 50676 A 2 .9 50676 A 1 <NA> 0 <NA> <NA> 0 ######################################################################################################## This is my attempt to add the group ID to the inserted line without success. y <- lapply(split(hcmid1, hcmid1$ID), function(x,.row){ .df <- data.frame(hcmid1$ID[1],.row$dose[2],.row$Avep1[3],.row$Avep2[4],.row$Avep3[5],.row$Avep4[6]) rbind(x, .df) # append the line to the original dataframe }) do.call(rbind, y) > y <- lapply(split(hcmid1, hcmid1$ID), function(x,.row){ + .df <- data.frame(cbind(hcmid1$ID[1],.row$dose[2],.row$Avep1[3],.row$Avep2[4],.row$Avep3[5 .... [TRUNCATED] Error in cbind(hcmid1$ID[1], .row$dose[2], .row$Avep1[3], .row$Avep2[4], : argument ".row" is missing, with no default ___________________________________________________________________________________ Also could somebody explain to me why I am getting 15 digits after the decimal on the printout when the input only has three decimal places and I am not doing any calculation yet on the data? I have set my options(digit=4). Any help on these questions are greatly appreciated. Thanks. I am using R2.8.0 with Win XP Pro John Lewis [[alternative HTML version deleted]] ______________________________________________ R-help@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.