要在数据框中添加一行,可以在下一个示例中进行操作:
> result <- rbind(baskets.df, c(7, 4))
> result
Granny Geraldine
1st 12 5
2nd 4 4
3rd 5 2
4th 6 4
5th 9 12
6th 3 9
7 7 4
如果要进一步,我想在新行中添加一个(数字)名称,让我们在2005年完成
> result <- rbind(baskets.df, "2005" = c(7, 4))
> result
Granny Geraldine
1st 12 5
2nd 4 4
3rd 5 2
4th 6 4
5th 9 12
6th 3 9
2005 7 4
但是如果我将2005保存在一个变量中,
> syear <- 2005
并且我希望新名称(在当前情况下为2005
)取决于分配给syear
的号码,怎么办?
如果我做出最自然的选择
> result <- rbind(baskets.df, as.character(syear) = c(7, 4))
我收到一个错误Error: unexpected '=' in "rbind(baskets.df, as.character(syear) ="
。
如果我尝试
> result <- rbind(baskets.df, syear = c(7, 4))
结果名称不是2005
,而是syear
。
您对我有什么建议?
谢谢!
答案 0 :(得分:1)
我们可以做作业
result[as.character(syear),] <- c(7, 4)
result <- structure(list(Granny = c(12L, 4L, 5L, 6L, 9L, 3L, 7L), Geraldine = c(5L,
4L, 2L, 4L, 12L, 9L, 4L)), class = "data.frame", row.names = c("1st",
"2nd", "3rd", "4th", "5th", "6th", "7"))