Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 从值列表向数据帧添加列_R - Fatal编程技术网

R 从值列表向数据帧添加列

R 从值列表向数据帧添加列,r,R,从一个空的数据帧开始,我需要按如下方式填充数据帧:for循环在每次迭代中生成固定数量的值,我需要使用该列表中的值添加一个新列,并给该列一个唯一的名称col_I(其中I是循环的第I次迭代) 如何完成这项看似简单的任务?cbind有什么问题 The functions cbind and rbind are S3 generic, with methods for data frames. The data frame method will be used if at least one arg

从一个空的数据帧开始,我需要按如下方式填充数据帧:for循环在每次迭代中生成固定数量的值,我需要使用该列表中的值添加一个新列,并给该列一个唯一的名称col_I(其中I是循环的第I次迭代)


如何完成这项看似简单的任务?

cbind有什么问题

The functions cbind and rbind are S3 generic, with methods for data frames. 
The data frame method will be used if at least one argument is a data frame 
and the rest are vectors or matrices. 

?colnames
也可以应用于数据。帧

有什么问题

The functions cbind and rbind are S3 generic, with methods for data frames. 
The data frame method will be used if at least one argument is a data frame 
and the rest are vectors or matrices. 

?colnames
也可以应用于数据。帧

分段构建数据帧的最有效方法是将部件存储在预先分配的列表中,然后将它们放在一起

例如:

num.iters <- 10
l <- vector('list', num.iters) 
for (i in 1:num.iters) {
     l[[i]] <- rnorm(3)                       # the column data
     names(l)[i] <- paste('Col', i, sep='.')  # the column name 
} 
do.call(cbind, l)  # ... if your cols are the same datatype and you want a matrix
data.frame(l)      # otherwise

num.iters分段构建数据帧的最有效方法是将您的部分存储在预先分配的列表中,然后将它们放在一起

例如:

num.iters <- 10
l <- vector('list', num.iters) 
for (i in 1:num.iters) {
     l[[i]] <- rnorm(3)                       # the column data
     names(l)[i] <- paste('Col', i, sep='.')  # the column name 
} 
do.call(cbind, l)  # ... if your cols are the same datatype and you want a matrix
data.frame(l)      # otherwise

num.iters如下所述,
cbind
是一个很好的起点。另外,请注意,一般来说,从一个空的数据结构开始并向其中添加内容是在R中做事情最糟糕的方式之一。一个好的参考是@user1030497的第2圈欢迎使用!如果以下任一答案解决了您的问题,请单击答案旁边的复选标记,将其视为已接受。如下所述,
cbind
是一个良好的起点。另外,请注意,一般来说,从一个空的数据结构开始并向其中添加内容是在R中做事情最糟糕的方式之一。一个好的参考是@user1030497的第2圈欢迎使用!如果以下任一答案解决了您的问题,请单击答案旁边的复选标记,将其视为已接受。