简体   繁体   中英

R: For calculating new variable R code

               id time bord    sex pbirth
              132 1255    1 Female     17
              132 1288    0      0     33
              172  985    1 Female     24
              172 1016    2 Female     31
              172 1054    3   Male     38
              172 1288    0      0    234

But, want to find this data. Where I want to add two new variables by conditioning on sex. If sex is equal to Female in a row then in next row nfemale=1 and if sex is equal Male in a row then in next row nmale=1 . It will split the data by id.

               id time bord    sex pbirth nfemale nmale
              132 1255    1 Female     17   0       0
              132 1288    0      0     33   1       0
              172  985    1 Female     24   0       0
              172 1016    2 Female     31   1       0
              172 1054    3   Male     38   2       0
              172 1288    0      0    234   2       1

By R code. Where, sex=0 , means missing value/ no observation, nfemale =No. of female before this time point and nmale =No. of female before this time point

You could use the function ddply from the plyr package. Assuming dat is the name of your data frame:

library(plyr)
ddply(dat, .(id), transform,
      nFemale = c(0, head(cumsum(sex == "Female"), -1)),
      nMale = c(0, head(cumsum(sex == "Male"), -1)))

   id time bord    sex pbirth nFemale nMale
1 132 1255    1 Female     17       0     0
2 132 1288    0      0     33       1     0
3 172  985    1 Female     24       0     0
4 172 1016    2 Female     31       1     0
5 172 1054    3   Male     38       2     0
6 172 1288    0      0    234       2     1
 dat$nfemale <- cumsum( c(0, dat$sex[1:(nrow(dat)-1)] =="Female"))
 dat$nmale <- cumsum( c(0, dat$sex[1:(nrow(dat)-1)] =="Male"))
 dat
#-----
   id time bord    sex pbirth nfemale nmale
1 132 1255    1 Female     17       0     0
2 132 1288    0      0     33       1     0
3 172  985    1 Female     24       1     0
4 172 1016    2 Female     31       2     0
5 172 1054    3   Male     38       3     0
6 172 1288    0      0    234       3     1

Doing it within categories which was only evident in the example and not in the sescription:

temp <- do.call(rbind, by(dat, dat$id, 
    function(d) data.frame(nfemale=cumsum( c(0, d$sex[1:(nrow(d)-1)] =="Female")), 
                           nmale=cumsum( c(0, d$sex[1:(nrow(d)-1)] =="Male")) ) ) )
      nfemale nmale
132.1       0     0
132.2       1     0
172.1       0     0
172.2       1     0
172.3       2     0
172.4       2     1

cbind(dat, temp)

Coming back here my solution stinks but I'll throw it up anyway (nice work DWin):

L1 <- split(dat, dat$id)
do.call(rbind.data.frame, lapply(L1, function(x){
    x[, "nfemale"] <- c(0, head(cumsum(x[, "sex"] == "Female"), -1))
    x[, "nmale"] <- c(0, head(cumsum(x[, "sex"] == "Male"), -1))
    x
}))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM