R数据集
x <- data.frame(term = c("A", "B", "C"))
x$points <- list(
data.frame(title=c("Iliad", "Odyssey"), value=c(-1194, -800)),
data.frame(title=c("Iliad", "Odyssey"), value=c(-44, -29)),
data.frame(title=c("Iliad", "Odyssey"), value=c(55, -700))
)
cat(toJSON(x, pretty=TRUE))所需结果
title A(value) B(value) C(value)
Iliad -1194 -44 55
Odyssey -800 -29 -700任何人都可以帮助我得到所需的结果
发布于 2017-03-14 03:04:44
library(tidyr)
unnest(x, points) %>%
spread(key = term, value = value)
# title A B C
# 1 Iliad -1194 -44 55
# 2 Odyssey -800 -29 -700当然,您可以使用paste将"(value)"字符串添加到列名中。我将把这个问题留给您(尽管我认为在列名中包含括号不是一个好主意)。
发布于 2017-03-14 03:33:38
类似这样的代码在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发布于 2017-03-14 11:29:40
以下是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 -700https://stackoverflow.com/questions/42771419
复制相似问题