Possible Duplicate:
R: losing column names when adding rows to an empty data frame可能的重复:R:在将行添加到空数据帧时丢失列名。
I created an empty dataframe with column names only as follows
我创建了一个空的dataframe,它的列名如下。
> compData <- data.frame(A= numeric(0), B= numeric(0))
> compData
[1] A B
<0 rows> (or 0-length row.names)
> compData <- rbind(compData,c(5,443))
> compData
X5 X443
1 5 443
in the above after adding one row the column names are changed. How can I add new row data to data-frame?
在上面添加一行后,列名被更改。如何向数据框架添加新的行数据?
6 个解决方案
#1
22
Adding to a zero-row data.frame
will act differently to adding to an data.frame
that already contains rows
添加到零行数据。frame将以不同的方式添加到已经包含行的数据。
From ?rbind
来自哪里? rbind
The rbind data frame method first drops all zero-column and zero-row arguments. (If that leaves none, it returns the first argument with columns otherwise a zero-column zero-row data frame.) It then takes the classes of the columns from the first data frame, and matches columns by name (rather than by position). Factors have their levels expanded as necessary (in the order of the levels of the levelsets of the factors encountered) and the result is an ordered factor if and only if all the components were ordered factors. (The last point differs from S-PLUS.) Old-style categories (integer vectors with levels) are promoted to factors.
rbind数据帧方法首先删除所有零列和零行参数。(如果没有,则返回第一个与列的参数,否则为零列的零行数据帧。)然后,它从第一个数据帧中获取列的类,并按名称匹配列(而不是按位置)。因素有必要(按照所遇到的因素的水平集的层次)进行扩展,结果是一个有序的因素,如果且仅当所有的成分都是有序的因素。(最后一点不同于S-PLUS。)老式的类别(具有层次的整数向量)被提升为因子。
You have a number of options --
你有很多选择。
the most straightforward
compData[1, ] <- c(5, 443)
more complicated
Or you could coerce c(5,433)
to a list or data.frame
或者您可以强制c(5,433)到一个列表或数据。
rbind(compData,setNames(as.list(c(5,443)), names(compData)))
or
或
rbind(compData,do.call(data.frame,setNames(as.list(c(5,443)), names(compData))))
But in this case you might as well do
但在这种情况下,你也可以这么做。
do.call(data.frame,setNames(as.list(c(5,443)), names(compData)))
data.table option
You could use the data.table
function rbindlist
which does less checking and thus preserves the names of the first data.frame
你可以使用这些数据。表函数rbindlist,它执行的检查较少,因此保留了第一个数据的名称。
library(data.table)
rbindlist(list(compData, as.list(c(5,443))
#2
11
I just got a simpler way to do it... as follows
我有一种更简单的方法…如下
compData <- data.frame(A= numeric(0), B= numeric(0))
compData
compData[nrow(compData)+1, ] <- c(5, 443)
compData
#3
4
Colnames <- names(compData)
compData <- rbind(compData, c(5, 443))
names(compData) <- Colnames
#4
2
You can assign to the dataframe by index:
您可以按索引分配给dataframe:
compData <- data.frame(A= numeric(0), B= numeric(0))
compData
compData[1, ] <- c(5, 443)
compData
Which gives:
这使:
> compData <- data.frame(A= numeric(0), B= numeric(0))
> compData
[1] A B
<0 rows> (or 0-length row.names)
> compData[1, ] <- c(5, 443)
> compData
A B
1 5 443
#5
2
You can use the function structure
with the .Names
argument:
您可以使用.Names参数的函数结构:
compData <- structure(rbind(compData,c(5,443)), .Names = names(compData))
# A B
#1 5 443
#6
2
If you have data of the same type*, you can do the following:
如果您有相同类型*的数据,您可以执行以下操作:
- Convert actual data frame to a matrix.
as.matrix(compData)
- 将实际数据帧转换为一个矩阵。as.matrix(compData)
- Add the new row to the end.
rbind(as.matrix(compData), c(5,443))
- 将新行添加到末尾。rbind(as.matrix(compData),c(5443))
- Convert the matrix back to a data frame.
as.data.frame(rbind(as.matrix(compData), c(5,443)))
- 将矩阵转换回数据帧。as.data.frame(rbind(as.matrix(compData),c(5443)))
In short:compData <- as.data.frame(rbind(as.matrix(compData), c(5,443)))
简而言之:compData <- as.data.frame(rbind(as.matrix, compData), c(5,443))
*If you have data of the same type, you might want to keep them in a matrix.
*如果您有相同类型的数据,您可能希望将它们保存在一个矩阵中。
#1
22
Adding to a zero-row data.frame
will act differently to adding to an data.frame
that already contains rows
添加到零行数据。frame将以不同的方式添加到已经包含行的数据。
From ?rbind
来自哪里? rbind
The rbind data frame method first drops all zero-column and zero-row arguments. (If that leaves none, it returns the first argument with columns otherwise a zero-column zero-row data frame.) It then takes the classes of the columns from the first data frame, and matches columns by name (rather than by position). Factors have their levels expanded as necessary (in the order of the levels of the levelsets of the factors encountered) and the result is an ordered factor if and only if all the components were ordered factors. (The last point differs from S-PLUS.) Old-style categories (integer vectors with levels) are promoted to factors.
rbind数据帧方法首先删除所有零列和零行参数。(如果没有,则返回第一个与列的参数,否则为零列的零行数据帧。)然后,它从第一个数据帧中获取列的类,并按名称匹配列(而不是按位置)。因素有必要(按照所遇到的因素的水平集的层次)进行扩展,结果是一个有序的因素,如果且仅当所有的成分都是有序的因素。(最后一点不同于S-PLUS。)老式的类别(具有层次的整数向量)被提升为因子。
You have a number of options --
你有很多选择。
the most straightforward
compData[1, ] <- c(5, 443)
more complicated
Or you could coerce c(5,433)
to a list or data.frame
或者您可以强制c(5,433)到一个列表或数据。
rbind(compData,setNames(as.list(c(5,443)), names(compData)))
or
或
rbind(compData,do.call(data.frame,setNames(as.list(c(5,443)), names(compData))))
But in this case you might as well do
但在这种情况下,你也可以这么做。
do.call(data.frame,setNames(as.list(c(5,443)), names(compData)))
data.table option
You could use the data.table
function rbindlist
which does less checking and thus preserves the names of the first data.frame
你可以使用这些数据。表函数rbindlist,它执行的检查较少,因此保留了第一个数据的名称。
library(data.table)
rbindlist(list(compData, as.list(c(5,443))
#2
11
I just got a simpler way to do it... as follows
我有一种更简单的方法…如下
compData <- data.frame(A= numeric(0), B= numeric(0))
compData
compData[nrow(compData)+1, ] <- c(5, 443)
compData
#3
4
Colnames <- names(compData)
compData <- rbind(compData, c(5, 443))
names(compData) <- Colnames
#4
2
You can assign to the dataframe by index:
您可以按索引分配给dataframe:
compData <- data.frame(A= numeric(0), B= numeric(0))
compData
compData[1, ] <- c(5, 443)
compData
Which gives:
这使:
> compData <- data.frame(A= numeric(0), B= numeric(0))
> compData
[1] A B
<0 rows> (or 0-length row.names)
> compData[1, ] <- c(5, 443)
> compData
A B
1 5 443
#5
2
You can use the function structure
with the .Names
argument:
您可以使用.Names参数的函数结构:
compData <- structure(rbind(compData,c(5,443)), .Names = names(compData))
# A B
#1 5 443
#6
2
If you have data of the same type*, you can do the following:
如果您有相同类型*的数据,您可以执行以下操作:
- Convert actual data frame to a matrix.
as.matrix(compData)
- 将实际数据帧转换为一个矩阵。as.matrix(compData)
- Add the new row to the end.
rbind(as.matrix(compData), c(5,443))
- 将新行添加到末尾。rbind(as.matrix(compData),c(5443))
- Convert the matrix back to a data frame.
as.data.frame(rbind(as.matrix(compData), c(5,443)))
- 将矩阵转换回数据帧。as.data.frame(rbind(as.matrix(compData),c(5443)))
In short:compData <- as.data.frame(rbind(as.matrix(compData), c(5,443)))
简而言之:compData <- as.data.frame(rbind(as.matrix, compData), c(5,443))
*If you have data of the same type, you might want to keep them in a matrix.
*如果您有相同类型的数据,您可能希望将它们保存在一个矩阵中。