简体   繁体   English

在r中绑定时,将嵌套的for循环的计数器值添加到每一行

[英]Add the counter value of nested for loop to each row while rbinding in r

I would like to be able create a new dataframe with 6 columns from an existing dataframe with 4 columns. 我希望能够从具有4列的现有数据框中创建一个具有6列的新数据框。 The two extra columns should be the value of the counters (i and j) whilst the loop is working. 循环工作时,额外的两列应为计数器的值(i和j)。

my draft code is as follows 我的草稿代码如下

a is binary, a是二进制的

b is categorical b是绝对的

c is a number (in this case 1 to 200) c是数字(在这种情况下为1到200)

d is a number (in this example 1 to 5, in real life 1 to 2500) d是一个数字(在此示例中为1到5,在现实生活中为1到2500)

#### make an example of mydata 
a<- c(0,0,0,0,0,0,0,0,0,0,1,1,0,1)
b<- c("a","b","a","b","b","c","a","e","c","a","a","b","d","f")
c<- c(20,30,40,40,54,76,23,23,78,23,34,1,88,1)
d<- c(1,1,1,2,2,2,3,3,4,5,5,5,5,5)
mydata<-data.frame(a,b,c,d)

## this just generates random numbers to randomly 
##select row to bind together later
set.seed(1)
choose.test<- data.frame(matrix(NA, nrow = 20, ncol = 30))
for (i in 1:20) 
{  
  choose.test[,i]<-sample(5, 20, replace = TRUE, prob = NULL) 
#random selction of sites WITH replacment
}

# this is the bit I am having trouble with
data<- NULL 
for( j in 1:10){
  for (i in choose.test[,j]) 
  {  data <- rbind(data, mydata[mydata[,4]== i,])
     data[,5]<-j
     data[,6]<-i  
  }}

It would also be acceptable to create separate dataframes at each loop iteration (in the second loop using i as a counter), or open to other better suggestions as I am new to r. 在每个循环迭代中创建单独的数据帧(在第二个循环中使用i作为计数器),也可以接受其他更好的建议,因为我是r的新手。 I also tried using assign to do this with no luck. 我也尝试使用assign来做到这一点。

At each iteration I need to rbind together all the rows in column 4 which have a value equal to a random number between 1 and 5 ( in this example anyway in real life it will be between 1 and 2500 sites). 在每次迭代中,我需要将第4列中所有行的值都绑定在一起,这些值等于1至5之间的随机数(在本示例中,无论如何,在现实生活中,它将在1至2500个站点之间)。 These random numbers are stored in a data frame, called choose.test , where the random numbers in each column is used only once then the next iteration moves onto the next column. 这些随机数存储在名为choose.test的数据框中,其中每列中的随机数仅使用一次,然后下一次迭代移至下一列。

Without the "data[,5]<-j data[,6]<-i" it does what almost what I want , but I would really like to have a 5th and 6th column that identify which iteration of the i and j loop the rows were from so I can analyse the data at each iteration (I am bootstrapping with this data). 没有“ data [,5] <-j data [,6] <-i”,它几乎可以实现我想要的功能,但是我真的很想拥有第5和第6列来标识i和j循环的哪个迭代这些行来自,因此我可以在每次迭代时分析数据(我正在使用该数据进行引导)。 Clearly the code above does not work, but I am not sure how to get it to do what I want. 显然,上面的代码不起作用,但是我不确定如何使它完成我想做的事情。 In the current version it just add the maximum counter value to all rows at columns 5 and 6. 在当前版本中,它仅将最大计数器值添加到第5列和第6列的所有行。

Many thanks, Ben 非常感谢,本

The following code fixed my problem 以下代码解决了我的问题

data<- NULL 
for( j in 1:10){
  for (i in choose.test[,j]) 
  {  data <- rbind(data, cbind(mydata[mydata[,4]== i,], i=i, j=j))}}

Credit goes to MrFlick for providing a useful comment! 感谢MrFlick提供有用的评论!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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