如何在零行(R中)的数据框中创建新列?

时间:2022-05-29 09:16:35

I am running a loop. In this loop new data frame(with same number and name of columns) is created everytime and is appended in the previous table. Ex: for i = 1

我正在运行一个循环。在此循环中,每次都会创建新数据框(具有相同的列数和名称),并将其附加到上一个表中。例如:因为i = 1

i have df1 = df1(n rows, m column)
df1$new_col <- "some value"
 df = rbind(df0,df1)
for i = 2 
i have df1 = df1(0 rows, m column)
df1$new_col <- "some val"
Error in `$<-.data.frame`(`*tmp*`, "new_col", value = "some_value") : 
  replacement has 1 row, data has 0

how do i add a new column to a df with zero rows ?

如何将新列添加到零行的df?

1 个解决方案

#1


1  

You can try something like this:

你可以尝试这样的事情:

df<-data.frame()
dim(df)
x<-1:100
df=rbind(df,x)
df=t(df)
dim(df)

#1


1  

You can try something like this:

你可以尝试这样的事情:

df<-data.frame()
dim(df)
x<-1:100
df=rbind(df,x)
df=t(df)
dim(df)