【问题标题】:Extract JSON-parsed variable from list within list从列表中的列表中提取 JSON 解析的变量
【发布时间】:2019-07-03 13:42:02
【问题描述】:

作为我论文的一部分,我正在分析政党的极性。在接收到 JSON 格式的 Facebook 消息的数据转储后,我将其解析为 R。不幸的是,嵌套了一个列表变量:

我需要从列表中列表中的列表中提取$sentiment$polarity$score

Observations: 63,465
Variables: 5
$ description <chr> "'TEXT'" ...
$ parties     <list> ["X", "X", "Y", ...
$ date        <date> 2018-03-05, 2018-03-05...
$ title       <chr> NA, NA...
$ sentiment   <list> [[[0.2998967, "Positief"], ...

使用glimpse(df$sentiment) 显示:

 $ :List of 2
  ..$ polarity    :List of 2
  .. ..$ score      : num 0.15
  .. ..$ description: chr "Neutraal"
  ..$ subjectivity:List of 2
  .. ..$ score      : num 0.65
  .. ..$ description: chr "Erg subjectief"
  [list output truncated]

编辑:head(df$sentiment, n=1) 给出:

[[1]]
[[1]]$`polarity`
[[1]]$`polarity`$`score`
[1] 0.2998967

[[1]]$`polarity`$description
[1] "Positief"

[[1]]$subjectivity
[[1]]$subjectivity$`score`
[1] 0.5458678

[[1]]$subjectivity$description
[1] "Subjectief"

但是,df$sentiment 的问题部分存在于(运行head(df$sentiment, n=10) 时)如下:

[[5]]
named list()

因此,观察确实包含一个空列表,而不是包含其他两个列表的格式。

我尝试了以下方法:

df %>% unnest(sentiment, .drop = FALSE, .sep = '"')

不幸的是,这使我的 df 翻了一番,从而失去了 polarity$scoresentiment$score 之间的区别。

我也试过了

matrix(unlist(df$sentiment),ncol=4,byrow=TRUE)

不幸的是,这无法处理 NULL 条目(即当$sentiment 为空而$polarity 不为空时)。因此,它创建了一个有缺陷的矩阵。

我也玩过flattenunlisttranpose 函数,但这似乎并没有让我有任何收获。我在 R 方面没有那么有经验,因此我希望有人可以帮助我提取正确的分数并将其作为列输入到我的数据框中。我希望我提供了所有需要的信息。

【问题讨论】:

  • 您能否提供更长的sentiment 示例。每个sentiment$polarity$score 的长度都是两个吗?您的代码matrix(unlist(df$sentiment), ncol = 4, byrow = TRUE) 似乎是一个好的开始,也许可以尝试创建一个if(length(df$sentiment) == 0){ rep(NA, 4)}。这可以捕获 NULL 的条目并用 NA 填充它,因此不会产生错误。
  • (1) sentiment$polarity$score 的每个观察值都包含一个介于 +1 和 -1 之间的数值。 (2) 上面的帖子中提供了通过head(df$sentiment) 的更长的情感示例。 (3) 我会在一分钟内运行你的尝试和更新!感谢您的建议。
  • 我试过你的建议@Rex,谢谢!不幸的是,这不起作用(因为条目不是“NULL”,而只是一个空列表)。我可以尝试使用flatten,但这放弃了$polarity$score$sentiment$score 之间的区别。

标签: r


【解决方案1】:

第一段代码是我创建一个示例。我通过设置 score = c() 将值设为 NULL 以查看它是否解决了您的问题。我确实必须用 for 循环来做,但它应该可以工作。第二位是如何使用数据框和列表值对其进行编码。它基本上会进行临时检查以测试 NULL 列表。

##construction of example data frame
a <- list(polarity = list(score = c(), description = "positief"))
b <- list(subjectivity = list(score = 2, description = "subjectief"))
c <- list(empty_list = list())
d <- list(c(a, b, c))

##my d is equivalent to your df
d[[1]][[1]][[1]]
length(d)
sent.pol.score <- double(length(d))
for ( i in 1 : length(d) ) {
    if ( length(d[[1]][[1]][[1]]) == 1 ) {
        sent.pol.score[i] <- d[[1]][[1]][[1]]
    }
}


##this should work with your data frame
sent.pol.score <- double(length(df$sentiment))
for ( i in 1 : length(df$sentiment) ) {
    if ( length(df$sentiment$polarity$score) == 1 ) {
        sent.pol.score[i] <- df$sentiment$polarity$score
    }
}

请注意,send.pol.score 将是数据集的长度,如果值为 NULL,则等于 0。我不知道这些可以取什么值,但您可能想将其更改为 sent.pol.score &lt;- rep(NA, length(df$sentiment))

【讨论】:

  • 感谢@Rex 的帮助和建议。唉,它不起作用,这迫使我进一步探索数据。在那里,我发现$sentiment 中存在空列表。因此,我设法通过以下方式实现了我的目标(请参阅更大的评论)。但是,我感谢您的所有帮助!
【解决方案2】:

在 Rex 的帮助下,我发现 $sentiment 中存在一些空列表(形式为 list() )。结合 Rex 的建议,我找到了以下解决方案:

#Remove empty lists from $sentiment
df.1 <- df %>% filter(sentiment != "list()")

#Unnest $sentiment list
df.2 <- df.1 %>% unnest(sentiment, .drop = FALSE, .sep = '"')

#Create function to remove even rows in df.2,  which contain $sentiment$subjectivity
Nth.delete <-function(dataframe, n)dataframe[-(seq(n,to=nrow(dataframe),by=n)),]

See: https://stackoverflow.com/questions/7942519/deleting-every-n-th-row-in-a-dataframe

#Execute Nth.delete function on every even rows of df, containing $sentiment$subjectivity
df.3 <- Nth.delete(df.1, 2)

#Unnest list $sentiment again to disctinct between $polarity$score and $polarity$description
df.4 <- df.3 %>% unnest(sentiment, .drop = FALSE, .sep = '"')

#Execute Nth.delete function again to remove the even rows containing $sentiment$polarity$description
df.5 <- Nth.delete(df.4, 2)

这创建了 df,其中$sentiment$polarity$score 在我的 df 中形成了一个连贯的列。

【讨论】:

  • 太棒了!很高兴你知道了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-11
  • 1970-01-01
  • 2021-02-01
  • 2021-12-29
  • 2021-11-14
相关资源
最近更新 更多