gpt4 book ai didi

r - R-将向量/矩阵作为行添加到另一个数据帧

转载 作者:行者123 更新时间:2023-12-03 08:23:57 26 4
gpt4 key购买 nike

我以为我很早以前就已经掌握了该技能,但是在这种情况下,我一无所知,我只会收到错误消息。

这是相同的问题,但对我不起作用!
How to add a named vector as a row to a data frame

我有这个数据框:

structure(list(AT2VI12 = 28, DENI011 = 27, GB0033R = 0, SE0004A = 2, 
SE0013R = 4), .Names = c("AT2VI12", "DENI011", "GB0033R",
"SE0004A", "SE0013R"), row.names = c(NA, -1L), class = "data.frame")

>df
AT2VI12 DENI011 GB0033R SE0004A SE0013R
1 28 27 0 2 4

现在,我想添加这个小第二数据框的第二列。
structure(list(x = c("AT2VI12", "DENI011", "GB0033R", "SE0004A", 
"SE0013R"), n = c(17L, 35L, 2L, 14L, 5L)), class = c("tbl_df",
"tbl", "data.frame"), .Names = c("x", "n"), row.names = c(NA,
-5L))

>df2
x n
<chr> <int>
1 AT2VI12 17
2 DENI011 35
3 GB0033R 2
4 SE0004A 14
5 SE0013R 5

我这样做:
df <- rbind(df, df2[,2])  # Error message: Not the same number of columns

然后,我尝试了此操作(希望稍后删除其他行)
df2 = t(df2)
df <- rbind(df, df2[2,]) # Error in match.names(clabs, names(xi))

和这个:
rbind(df, as.data.frame(t(df2)))

我不知道怎么了。有人可以帮忙吗?

最佳答案

这是一个tibble,无论您使用哪种索引,它都将始终返回tibble,因此您需要将其取消列出以获取 vector :

 rbind(df,unlist(df2[2]))
AT2VI12 DENI011 GB0033R SE0004A SE0013R
1 28 27 0 2 4
2 17 35 2 14 5

关于r - R-将向量/矩阵作为行添加到另一个数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48631147/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com