R:展开现有数据框中的数据框列

时间:2017-03-28 21:46:31

标签: r dataframe jsonlite

我正在使用jsonlite::fromJSON将JSON文档提供给数据框。问题是它没有完全变平 - 由于我不知道的原因(但是下面有人提到因为它们是字典的形式,它们不会被弄平)。

df <- jsonlite::fromJSON(out, flatten = TRUE, simplifyDataFrame = TRUE)
n <- df$hits$total # 1 entry
dat <- df$hits$hits

# dat is a dataframe
dim(dat)
[1]  2 5

# class of each column in the dataframe
lapply(dat, class)

$`_index`
[1] "character"

$`_type`
[1] "character"

$`_id`
[1] "character"

$`_score`
[1] "numeric"

$`_source.samples`
[1] "list"

dat$_source.samples实际上是数据帧列表:

> dim(dat$`_source.samples`[[1]])
[1] 21  2
> dim(dat$`_source.samples`[[2]])
[1] 21  2

如何展开包含数据框的列,使其在现有数据框中形成两个新列 - 并且由于扩展而复制前四行。这是一个例子:

Rstudio截图dat看起来如何: Rstudio screenshot of dat

# the first four columns look like this
> head(dat[,1:4])
  _index _type                _id _score
1   pnoc genes ENSG00000131051.20      1
2   pnoc genes ENSG00000000457.13      1

# the fifth column `_source.samples` that has dataframes looks like this 
# just showing the dataframe in the first row of `dat`
> head(dat$`_source.samples`[[1]])
                        sample_id rsem.fpkm
1 C021_0001_20140916_tumor_RNASeq     39.11
2        CPBT_0001_1_tumor_RNASeq    184.56
3        CPBT_0007_1_tumor_RNASeq     41.29
4   C021_0010_001774_tumor_RNASeq     86.31
5   C021_0003_001409_tumor_RNASeq     79.24
6        CPBT_0005_1_tumor_RNASeq     66.20

所以我想要这样的东西:

 _index _type                _id _score                       sample_id
1   pnoc genes ENSG00000000457.13      1 C021_0001_20140916_tumor_RNASeq
2   pnoc genes ENSG00000000457.13      1        CPBT_0001_1_tumor_RNASeq
3   pnoc genes ENSG00000000457.13      1        CPBT_0007_1_tumor_RNASeq
4   pnoc genes ENSG00000000457.13      1   C021_0010_001774_tumor_RNASeq
5   pnoc genes ENSG00000000457.13      1   C021_0003_001409_tumor_RNASeq
6   pnoc genes ENSG00000000457.13      1        CPBT_0005_1_tumor_RNASeq
  rsem.fpkm
1      1.39
2      5.58
3      1.93
4      3.64
5      5.20
6      3.69

这是一个可重现的数据集:

> dput(dat)
structure(list(`_index` = c("pnoc", "pnoc"), `_type` = c("genes", 
"genes"), `_id` = c("ENSG00000131051.20", "ENSG00000000457.13"
), `_score` = c(1, 1), `_source.samples` = list(structure(list(
    sample_id = c("C021_0001_20140916_tumor_RNASeq", "CPBT_0001_1_tumor_RNASeq", 
    "CPBT_0007_1_tumor_RNASeq", "C021_0010_001774_tumor_RNASeq", 
    "C021_0003_001409_tumor_RNASeq", "CPBT_0005_1_tumor_RNASeq", 
    "CPBT_0008_1_tumor_RNASeq", "C021_0002_001113_tumor_RNASeq", 
    "C021_0013_001872_tumor_RNASeq", "C021_0005_001661_tumor_RNASeq", 
    "C021_0007_001669_tumor_RNASeq", "C021_0008_001699_tumor_RNASeq", 
    "CPBT_0006_1_tumor_RNASeq", "C021_0011_001786_tumor_RNASeq", 
    "C021_0009_001766_tumor_RNASeq", "CPBT_0004_1_tumor_RNASeq", 
    "CPBT_0003_1_tumor_RNASeq", "CPBT_0009_1_tumor_RNASeq", "C021_0006_001666_tumor_RNASeq", 
    "C021_0012_001825_tumor_RNASeq", "C021_0004_001418_tumor_RNASeq"
    ), rsem.fpkm = c(39.11, 184.56, 41.29, 86.31, 79.24, 66.2, 
    42.13, 88.78, 78.73, 96.79, 38.5, 105.12, 129.16, 145.13, 
    117.96, 86.53, 75.43, 179.01, 0, 61.61, 98.64)), .Names = c("sample_id", 
"rsem.fpkm"), class = "data.frame", row.names = c(NA, 21L)), 
    structure(list(sample_id = c("C021_0001_20140916_tumor_RNASeq", 
    "CPBT_0001_1_tumor_RNASeq", "CPBT_0007_1_tumor_RNASeq", "C021_0010_001774_tumor_RNASeq", 
    "C021_0003_001409_tumor_RNASeq", "CPBT_0005_1_tumor_RNASeq", 
    "CPBT_0008_1_tumor_RNASeq", "C021_0002_001113_tumor_RNASeq", 
    "C021_0013_001872_tumor_RNASeq", "C021_0005_001661_tumor_RNASeq", 
    "C021_0007_001669_tumor_RNASeq", "C021_0008_001699_tumor_RNASeq", 
    "CPBT_0006_1_tumor_RNASeq", "C021_0011_001786_tumor_RNASeq", 
    "C021_0009_001766_tumor_RNASeq", "CPBT_0004_1_tumor_RNASeq", 
    "CPBT_0003_1_tumor_RNASeq", "CPBT_0009_1_tumor_RNASeq", "C021_0006_001666_tumor_RNASeq", 
    "C021_0012_001825_tumor_RNASeq", "C021_0004_001418_tumor_RNASeq"
    ), rsem.fpkm = c(1.39, 5.58, 1.93, 3.64, 5.2, 3.69, 1.75, 
    5.38, 3.46, 4.14, 0.96, 3.93, 4.47, 3.17, 4.38, 2.8, 2.27, 
    7.4, 0, 2.76, 5.55)), .Names = c("sample_id", "rsem.fpkm"
    ), class = "data.frame", row.names = c(NA, 21L)))), .Names = c("_index", 
"_type", "_id", "_score", "_source.samples"), class = "data.frame", row.names = 1:2)

谢谢!

1 个答案:

答案 0 :(得分:1)

这些列不会变平,因为它们最初是在json文件中嵌入的词典。

这是你可以做的,虽然它不是完全直观的:

首先,您使用列_source.samples的内容创建新的数据框。您需要确保保留_id列以确定每个数据来自哪一行。

samples <- mapply(function(x, y) cbind(x, _id = y), dat$`_source.samples, dat$_id, SIMPLIFY = FALSE)

然后将其与dat合并。

merge(dat, rbind.pages(samples), by= "_id")