R:向空数据框添加行时丢失列名
我刚从R开始,遇到了一个奇怪的行为:在空数据框中插入第一行时,原始列名丢失 例如:R:向空数据框添加行时丢失列名,r,dataframe,names,rbind,R,Dataframe,Names,Rbind,我刚从R开始,遇到了一个奇怪的行为:在空数据框中插入第一行时,原始列名丢失 例如: a<-data.frame(one = numeric(0), two = numeric(0)) a #[1] one two #<0 rows> (or 0-length row.names) names(a) #[1] "one" "two" a<-rbind(a, c(5,6)) a # X5 X6 #1 5 6 names(a) #[1] "X5" "X6" a解决方法是
a<-data.frame(one = numeric(0), two = numeric(0))
a
#[1] one two
#<0 rows> (or 0-length row.names)
names(a)
#[1] "one" "two"
a<-rbind(a, c(5,6))
a
# X5 X6
#1 5 6
names(a)
#[1] "X5" "X6"
a解决方法是:
a <- rbind(a, data.frame(one = 5, two = 6))
a帮助页面指定:
对于'cbind'('rbind'),向量为零
忽略长度(包括“NULL”)
除非结果有零行
(列),以便与S兼容。
(零范围矩阵不会出现在
S3和在R中不被忽略。)
因此,实际上,a
在rbind
指令中被忽略。似乎没有完全忽略,因为它是一个数据帧,rbind
函数被称为rbind.data.frame
:
rbind.data.frame(c(5,6))
# X5 X6
#1 5 6
插入行的一种方法可能是:
a[nrow(a)+1,] <- c(5,6)
a
# one two
#1 5 6
a[nrow(a)+1,]FWIW,另一种设计可能会让您的函数为两列构建向量,而不是绑定到数据帧:
ones <- c()
twos <- c()
d_dataset <-
data.frame(
variable = character(),
before = numeric(),
after = numeric(),
stringsAsFactors = FALSE)
d_dataset <-
rbind(
d_dataset,
data.frame(
variable = "test",
before = 9,
after = 12,
stringsAsFactors = FALSE))
print(d_dataset)
variable before after
1 test 9 12
one几乎屈服于这个问题
1) 创建数据框时,将stringsAsFactor
设置为FALSE
,否则直接进入下一期
2) 不要使用rbind
——不知道为什么它会把列名搞乱。只需这样做:
df[nrow(df)+1,]您可以这样做:
为初始数据帧指定一行
df=data.frame(matrix(nrow=1,ncol=length(newrow))
添加新行并取出NAS
newdf=na.omit(rbind(newrow,df))
但请注意,您的新行没有NAs,否则它也将被擦除
干杯
Agus我使用作为.numeric(0)
而不是用构建data.frame
绑定附加行
a<-rbind(a,c(5,6))
a
# one two
#1 0 0
#2 5 6
a使这项工作通用且只需最少的重新键入列名的方法如下。此方法不需要对NA或0进行黑客攻击
rs <- data.frame(i=numeric(), square=numeric(), cube=numeric())
for (i in 1:4) {
calc <- c(i, i^2, i^3)
# append calc to rs
names(calc) <- names(rs)
rs <- rbind(rs, as.list(calc))
}
另一种更干净的方法是使用data.table:
> df <- data.frame(a=numeric(0), b=numeric(0))
> rbind(df, list(1,2)) # column names are messed up
> X1 X2
> 1 1 2
> df <- data.table(a=numeric(0), b=numeric(0))
> rbind(df, list(1,2)) # column names are preserved
a b
1: 1 2
我使用以下解决方案将行添加到空数据框:
ones <- c()
twos <- c()
d_dataset <-
data.frame(
variable = character(),
before = numeric(),
after = numeric(),
stringsAsFactors = FALSE)
d_dataset <-
rbind(
d_dataset,
data.frame(
variable = "test",
before = 9,
after = 12,
stringsAsFactors = FALSE))
print(d_dataset)
variable before after
1 test 9 12
d_数据集我认为在你的代码中,rbind
中的a
被忽略了,因此它实际上相当于a+1我通常使用这种方法——注意,你可以简单地将a
初始化为空向量:a@juba,可能是这样,因为data.framea
是空的。非常有用。也许没有那么简洁,但数据流没有那么黑盒。这确实是一个很好的答案。但它似乎很“不R”。在构造data.frame时,首先需要循环所有内容,而行运算符是R的工作马。可能使用@juba的答案,但在末尾设置colnames:colnames(a)这种方法的问题是,通常需要colnames来扩展数据帧。为什么r中这么简单的事情这么复杂?大多数情况下可能是这样。请注意,使用该方法,c
列不再是数字!str(df)表示它是字符。如果您有不同的数据类型(character
和numeric
),最好使用list
函数list(“五”,6)
)。否则它会把一切都理解为性格。
a<-a[-1,]
a
# one two
#2 5 6
rs <- data.frame(i=numeric(), square=numeric(), cube=numeric())
for (i in 1:4) {
calc <- c(i, i^2, i^3)
# append calc to rs
names(calc) <- names(rs)
rs <- rbind(rs, as.list(calc))
}
> rs
i square cube
1 1 1 1
2 2 4 8
3 3 9 27
4 4 16 64
>
> df <- data.frame(a=numeric(0), b=numeric(0))
> rbind(df, list(1,2)) # column names are messed up
> X1 X2
> 1 1 2
> df <- data.table(a=numeric(0), b=numeric(0))
> rbind(df, list(1,2)) # column names are preserved
a b
1: 1 2
> class(df)
"data.table" "data.frame"
d_dataset <-
data.frame(
variable = character(),
before = numeric(),
after = numeric(),
stringsAsFactors = FALSE)
d_dataset <-
rbind(
d_dataset,
data.frame(
variable = "test",
before = 9,
after = 12,
stringsAsFactors = FALSE))
print(d_dataset)
variable before after
1 test 9 12