簡體   English   中英

如何映射r中列表子列表中的值

[英]How to map values in sublist of list in r

我是相當新的R.我正在嘗試將函數應用於我得到的子列表中的值,但我不知道在哪里看。

我在R中讀了一些json文件,如下所示:

library("rjson")
fileNames <- list.files(path = "/<PATH>/TimedSentences", full.names = TRUE)
parseJson <- function(fileName){
  fromJSON(file = fileName)
}

jsons <- lapply(fileNames, parseJson)

jsonToSentJson <- function(jsonList){
  #change sentences to sentiments
}

json文件看起來有點像這樣

{
  "name": "<STORYNAME>",
  "sentences": [
    {
      "beginTime": 880,
      "endTime": 16960,
      "sentence": "Okay, guys, here we go."
    },
    {
      "beginTime": 14160,
      "endTime": 16960,
      "sentence": "Here we go."
    },...]
}

現在我希望得到幾乎相同的列表,除了我想針對每個句子值運行一個函數並將其更改為情緒值。 我安裝了一個包含一個名為“get_sentiment”的函數的軟件包,我基本上寫了一個看起來像這樣的json文件:

{
  "name": "<MOVIENAME>",
  "sentences": [
    {
      "beginTime": 880,
      "endTime": 16960,
      "sentiment": 1.5
    },
    {
      "beginTime": 14160,
      "endTime": 16960,
      "sentiment": 0.0
    },...]
}

我在嘗試這樣做時應該注意什么?

注意:我可以弄清楚如何編寫jsn文件,但我無法弄清楚如何更改子列表中的值

我想到了

我做到了

jsonSentencesToJsonSentiment <- function(json){
    list("name" = json[["name"]], 
     "sentiments" =lapply(json[["sentences"]], timedSentenceToTimedSentiment)
     )
}

timedSentenceToTimedSentiment <- function(timedSentence){
  list("beginTime" = timedSentence[["beginTime"]], 
    "endTime" = timedSentence[["endTime"]],
    "sentiment" = get_sentiment(c(timedSentence[["sentence"]]), method = "nrc"))
}

sentJson <- lapply(jsons, jsonSentencesToJsonSentiment)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM