在 R 中使用具体数字名称向数据框添加一行
Adding a row to a data frame with a concrete numeric name in R
要向数据框添加一行,我可以在下一个示例中这样做:
> 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
。
你对我有什么建议?
谢谢!
我们可以做一个作业
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"))
要向数据框添加一行,我可以在下一个示例中这样做:
> 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
。
你对我有什么建议?
谢谢!
我们可以做一个作业
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"))