使用for循环i R根据循环中创建的列创建新列

时间:2017-04-28 14:14:06

标签: r for-loop

我试图模拟三个不同比例接受治疗的患者死亡的十年风险。我已经每年这样做十年了,结果证明它是一个很长的代码。我想要的是将其转换为每月十年,并且为了避免数百行代码,我想使用for循环。

我的数据看起来像这样

set.seed(1234)
N <- 750000

id <- c(1:N)

###creates a sex variable for men and appends women
treated <- rep.int(0,125000)
treated <- append(treated, rep.int(1,125000))
treated <- append(treated, rep.int(0,100000))
treated <- append(treated, rep.int(1,150000))
treated <- append(treated, rep.int(0,75000))
treated <- append(treated, rep.int(1,175000))

groupname <- rep.int(1,250000)
groupname <- c(groupname, rep.int(2,250000))
groupname <- c(groupname, rep.int(3,250000))  

从性别和id矢量

创建数据帧
data = data.frame(treated, id, groupname)
class(data$treated)
data$treated <- factor(data$treated, levels = c(0,1), labels = c("untreated","treated"))
data$groupname <- factor(data$groupname, levels = c(1,2,3), labels = c("group 1", "group 2", "group 3"))

然后我生成每个“wave”,就像这样十年(基本相同的代码,只为每个波分配一个新的列名):

data$year_0 <- 1
data$year_1 <-  ifelse(data$treated=="treated",rbinom(N, 1, 1-0.035/4), rbinom(N, 1, 1-0.05/4))

data$year_2 <- ifelse(data$treated=="treated", 
                      ifelse(data$year_1 =="0",  0, rbinom(N, 1, 1-0.035/4)), 
                      ifelse(data$year_1 =="0",  0, rbinom(N, 1, 1-0.05/4))
)
data$year_3 <- ifelse(data$treated=="treated", 
                      ifelse(data$year_2 =="0",  0, rbinom(N, 1, 1-0.035/4)), 
                      ifelse(data$year_2 =="0",  0, rbinom(N, 1, 1-0.05/4))
)
data$year_4 <- ifelse(data$treated=="treated", 
                      ifelse(data$year_3 =="0",  0, rbinom(N, 1, 1-0.035/4)), 
                      ifelse(data$year_3 =="0",  0, rbinom(N, 1, 1-0.05/4))
)
data$year_5 <- ifelse(data$treated=="treated", 
                      ifelse(data$year_4 =="0",  0, rbinom(N, 1, 1-0.035/4)), 
                      ifelse(data$year_4 =="0",  0, rbinom(N, 1, 1-0.05/4))
)
data$year_6 <- ifelse(data$treated=="treated", 
                      ifelse(data$year_5 =="0",  0, rbinom(N, 1, 1-0.035/4)), 
                      ifelse(data$year_5 =="0",  0, rbinom(N, 1, 1-0.05/4))
)
data$year_7 <- ifelse(data$treated=="treated", 
                      ifelse(data$year_6 =="0",  0, rbinom(N, 1, 1-0.035/4)), 
                      ifelse(data$year_6 =="0",  0, rbinom(N, 1, 1-0.05/4))
)
data$year_8 <- ifelse(data$treated=="treated", 
                      ifelse(data$year_7 =="0",  0, rbinom(N, 1, 1-0.035/4)), 
                      ifelse(data$year_7 =="0",  0, rbinom(N, 1, 1-0.05/4))
)
data$year_9 <- ifelse(data$treated=="treated", 
                      ifelse(data$year_8 =="0",  0, rbinom(N, 1, 1-0.035/4)), 
                      ifelse(data$year_8 =="0",  0, rbinom(N, 1, 1-0.05/4))
)
data$year_10 <- ifelse(data$treated=="treated", 
                       ifelse(data$year_9 =="0",  0, rbinom(N, 1, 1-0.035/4)), 
                       ifelse(data$year_9 =="0",  0, rbinom(N, 1, 1-0.05/4))
)
###converts to long format
data_long <- reshape(data, direction="long", varying= c(list(4:14)), sep = "_", 
                     idvar="id", timevar=c("year"))
class(data_long$year)  
data_long$year <- as.numeric(data_long$year)
data_long$year <- data_long$year -1

我想用for循环来做这个,所以我可以模拟120个月 我写了这段代码

for (i in 1:10){ n <- ifelse(data$treated=="treated", 
                                      ifelse(data$year_[(i-1)] =="0",  0, rbinom(N, 1, 1-0.035/4)), 
                                      ifelse(data$year_[(i-1)] =="0",  

0, rbinom(N, 1, 1-0.05/4))

                                 )
              data$year_[i] <- n 
    }

##1: I data$year_[i] <- n :

##error number of items to replace is not a multiple of replacement length

据我所知,此错误表示循环编码的方式返回不兼容的长度数据。通常我可以通过谷歌进行故障排除,但是因为代码在我不在for循环中时运行 我不明白问题出在哪里。 我认为错误可能是在[i]的解释中,而不是可用于命名列的字符串,但除了已经提到的字符之外,使用粘贴只会导致此警告。

##Fejl i `$<-.data.frame`(`*tmp*`, "year_", value = c(NA, NA, NA, NA,  : 
  ##replacement has 750001 rows, data has 750000 

并且谷歌在这个问题上的结果似乎并没有将此视为一个问题。 所以现在的问题是,我不知道问题是什么。

2 个答案:

答案 0 :(得分:0)

您可以将列year_i放在一个额外的矩阵中。然后,您可以使用cbind()按列扩展矩阵列:

set.seed(1234)
N <- 750000

data = data.frame(treated=rep(c(0,1,0,1,0,1), c(125000, 125000, 100000, 150000, 75000, 175000)), id=1:N, 
groupname=rep(1:3, each=250000))
data$treated <- factor(data$treated, levels = c(0,1), labels = c("untreated","treated"))
data$groupname <- factor(data$groupname, levels = c(1,2,3), labels = c("group 1", "group 2", "group 3"))

Year <- matrix(1, N, 1) # data$year_0 <- 1
Year <- cbind(Year, ifelse(data$treated=="treated",rbinom(N, 1, 1-0.035/4), rbinom(N, 1, 1-0.05/4))) # data$year_1
for (i in 2:10) {
  lastcol <- Year[,ncol(Year)]
  Year <- cbind(Year,
                ifelse(data$treated=="treated", 
                       ifelse(lastcol==0,  0, rbinom(N, 1, 1-0.035/4)), 
                       ifelse(lastcol==0,  0, rbinom(N, 1, 1-0.05/4)))
                )
}

你可以通过预分配加快一点(但最重要的是采样):

set.seed(1234)
K <- 10 # year_0 ... year_K
Year <- matrix(NA, N, K+1)
Year[,1] <- 1  # year_0
Year[,2] <- ifelse(data$treated=="treated", rbinom(N, 1, 1-0.035/4), rbinom(N, 1, 1-0.05/4)) # data$year_1
for (i in 3:(K+1)) Year[,i] <- ifelse(data$treated=="treated", 
                                      ifelse(Year[,i-1]==0,  0, rbinom(N, 1, 1-0.035/4)), 
                                      ifelse(Year[,i-1]==0,  0, rbinom(N, 1, 1-0.05/4)))

如果需要,可以将数据框和矩阵Year放在一起。如果是这样,最好将列名分配给矩阵:

colnames(Year) <- paste0("year_", 0:K)

答案 1 :(得分:0)

考虑使用括号引用[[...]]到列名称来传递带有paste0()的字符串,条件为第一年,然后是所有其他年份:

data$year_0 <- 1

for (i in 1:10){ 
  if (i == 1){
     n <- ifelse(data$treated=="treated", rbinom(N, 1, 1-0.035/4), rbinom(N, 1, 1-0.05/4))
  } 
  else {
     n <- ifelse(data$treated=="treated", 
                 ifelse(data[[paste0("year_", i-1)]] == 0,  0, rbinom(N, 1, 1-0.035/4)), 
                 ifelse(data[[paste0("year_", i-1)]] == 0,  0, rbinom(N, 1, 1-0.05/4))
          )
  }
  data[[paste0("year_", i)]] <- n 
}