数据框排列/重新排列和删除重复列 R数据集
任何人都可以帮助我获得所需的结果数据框排列/重新排列和删除重复列 R数据集,r,join,dataframe,duplicates,R,Join,Dataframe,Duplicates,任何人都可以帮助我获得所需的结果 library(tidyr) unnest(x, points) %>% spread(key = term, value = value) # title A B C # 1 Iliad -1194 -44 55 # 2 Odyssey -800 -29 -700 当然,您可以使用粘贴将(值)字符串添加到列名中。我将把它留给您(尽管我认为在列名中包含括号是个坏主意)。类似的内容在base R中也应该适用: c
library(tidyr)
unnest(x, points) %>%
spread(key = term, value = value)
# title A B C
# 1 Iliad -1194 -44 55
# 2 Odyssey -800 -29 -700
当然,您可以使用粘贴将
(值)
字符串添加到列名中。我将把它留给您(尽管我认为在列名中包含括号是个坏主意)。类似的内容在base R
中也应该适用:
cbind.data.frame(title=x$points[[1]][,1],
do.call(cbind, lapply(1:length(x$term),
function(i) setNames(data.frame(x$points[[i]][,2]), x$term[i]))))
# title A B C
#1 Iliad -1194 -44 55
#2 Odyssey -800 -29 -700
这里有一个带有
merge
和Reduce的base R
选项
setNames(Reduce(function(...) merge(..., by = 'title'), x$points),
c('title', as.character(x$term)))
# title A B C
#1 Iliad -1194 -44 55
#2 Odyssey -800 -29 -700
很好的可复制的例子!我鼓励你们下次也尝试用文字描述你们想要做什么。你们正在寻找加入三个数据框架。感谢Gregor的解决方案和快速响应
cbind.data.frame(title=x$points[[1]][,1],
do.call(cbind, lapply(1:length(x$term),
function(i) setNames(data.frame(x$points[[i]][,2]), x$term[i]))))
# title A B C
#1 Iliad -1194 -44 55
#2 Odyssey -800 -29 -700
setNames(Reduce(function(...) merge(..., by = 'title'), x$points),
c('title', as.character(x$term)))
# title A B C
#1 Iliad -1194 -44 55
#2 Odyssey -800 -29 -700