简体   繁体   中英

Creating new variable by summing the columns of the data frame using for loop

First, I want to simulate let 20 data sets using for loop. Once these data sets are generated, I want to add a new variable (column) to these data sets on each iteration. The new variable is the sum of all columns of each data set.

library(bindata)
set.seed(485)
cor.mat = diag(1, nrow = 3)
for (i in 1:nrow(cor.mat)) {
  for (j in 1:ncol(cor.mat)) {
    if (i > j) cor.mat[i, j] = runif(1, 0.2, 0.7)
    cor.mat[j, i] = cor.mat[i, j]
  }
}

N=20
mydata = list()
var = list()
mydata.new = list()
for(i in 1:N){
    mydata[[i]] = rmvbin(n=10,margprob=rep(0.4,3),bincorr = cor.mat)
    var[[i]] = mydata[,1][i]+mydata[,2][i]+mydata[,3][i] # I need your help here
    mydata.new[[i]] = cbind(mydata,var) # Again I need your help here
}

I expect 20 mydata.new data sets to be like the following.

v1 v2 v3 var
0  1  0   1
1  0  0   1
1  1  1   3
0  0  0   0
1  0  1   2

Thank you in advance!

If I understand correctly, you want this:

for(i in 1:N){
  tmp = rmvbin(n=10,margprob=rep(0.4,3),bincorr = cor.mat)
  mydata[[i]] <- cbind(tmp, rowSums(tmp))
}

> mydata
[[1]]
      [,1] [,2] [,3] [,4]
 [1,]    0    0    0    0
 [2,]    0    1    0    1
 [3,]    0    1    0    1
 [4,]    0    0    0    0
 [5,]    0    0    1    1
 [6,]    1    1    1    3
 [7,]    0    0    0    0
 [8,]    1    1    1    3
 [9,]    0    1    1    2
[10,]    0    0    0    0
...

Does this deliver the desired results?

# create some dummy-data for test
mydata = replicate(20, list(matrix(runif(9, 1, 10), ncol = 3)))

    class(mydata)
    length(mydata)

    # to get rowsums for each element of the list we can use lapply in combination with rowsums
    # and use cbind to attach it to the current list-object.
    mydata.new = lapply(mydata, function(f) cbind(f, rowSums(f)))

    # check results
    mydata.new[1]
    rowSums(mydata[[1]])

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