【问题标题】:R/Plotly: Error in list2env(data) : first argument must be a named listR / Plotly:list2env(数据)中的错误:第一个参数必须是命名列表
【发布时间】:2016-04-29 22:15:42
【问题描述】:

我在使用 R 方面经验丰富,但我才刚刚开始学习编写函数来自动执行任务。我目前正在开展一个项目,对剩下的五位总统候选人的演讲进行情绪分析和主题模型,但遇到了障碍。

我写了一个function,对正面和负面情绪进行逐句分析,给每个句子打分。奇迹般地,它奏效了,并为我提供了一个数据框,其中包含每个句子的分数。

    score        text
1     1        iowa, thank you.
2     2        thanks to all of you here tonight for your patriotism, for your love of country and for doing what too few americans today are doing.  
3     0        you are not standing on the sidelines complaining. 
4     1        you are not turning your backs on the political process.
5     2        you are standing up and fighting back.

所以我现在要做的是创建一个function,它获取分数并计算出每个分数的计数代表的总百分比,然后使用plotly 绘制它。所以这是我写的函数:

scoreFun <- function(x){{
  tbl <- table(x)
  res <- cbind(tbl,round(prop.table(tbl)*100,2))
  colnames(res) <- c('Score', 'Count','Percentage')
  return(res)
}
  percent = data.frame(Score=rownames, Count=Count, Percentage=Percentage)
  return(percent)
}

返回这个:

saPct <- scoreFun(sanders.scores$score)
saPct

     Count Percentage
-6     1       0.44
-5     1       0.44
-4     6       2.64
-3    13       5.73
-2    20       8.81
-1    42      18.50
0     72      31.72
1     34      14.98
2     18       7.93
3      9       3.96
4      6       2.64
5      2       0.88
6      1       0.44
9      1       0.44
11     1       0.44

我希望它返回的是一个数据框,其中最终将 rownames 作为一个名为 Score 的变量,接下来的两列分别称为 CountPercentage。然后我想使用以下代码在 x 轴上绘制 Score 和在 y 轴上绘制 Percentage

d <- subplot(
  plot_ly(clPct, x = rownames, y=Percentage, xaxis="x1", yaxis="y1"),
  plot_ly(saPct, x = rownames, y=Percentage, xaxis="x2", yaxis="y2"),
  margin = 0.05,
  nrows=2
) %>% layout(d, xaxis=list(title="", range=c(-15, 15)),
             xaxis2=list(title="Score", range=c(-15,15)),
             yaxis=list(title="Clinton", range=c(0,50)),
             yaxis2=list(title="Sanders", range=c(0,50)),showlegend = FALSE)
d

我很确定我在我的function 和我的plot_ly 代码中犯了一些明显的错误,因为很明显它没有返回我想要的数据帧并且在我运行时导致错误Error in list2env(data) : first argument must be a named list `情节代码。不过,我在编写函数方面并不是很有经验,而且我在谷歌时也没有发现类似的问题,所以我不知道如何解决这个问题。

欢迎任何建议。谢谢!

【问题讨论】:

  • str(saPct) 返回什么?
  • num [1:15, 1:2] 1 1 6 13 20 42 72 34 18 9 ... - attr(*, "dimnames")=List of 2 ..$ : chr [1:15] "-6" "-5" "-4" "-3" ... ..$ : chr [1:2] "Count" "Percentage"
  • 所以它是列表,你需要把它变成一个数据框(clPct也是)
  • 谢谢!我把它变成了一个数据框,但我丢失了行名,这是我想要用于我的情节的scores。然后,我尝试回溯并重新运行 scoreFun 函数,但它现在无法正常工作。它给了我这个错误:'names' attribute [3] must be the same length as the vector [2]。我的 prop.table 没有将行名作为向量的一部分返回。我从this question 获得了部分代码。那里还有其他代码示例,所以我会尝试它们。再次感谢!

标签: r function plot plotly


【解决方案1】:

@MLavoie,我在评论中引用的问题中的这段代码起到了作用。非常感谢!

scoreFun <- function(x){
  tbl <- data.frame(table(x))
  colnames(tbl) <- c("Score", "Count")
  tbl$Percentage <- tbl$Count / sum(tbl$Count) * 100
  return(tbl)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-12
    相关资源
    最近更新 更多