【问题标题】:R - Error when writing to csvR - 写入 csv 时出错
【发布时间】:2017-01-26 14:34:44
【问题描述】:

我有以下数据集:

 Artist Song.Rank          Song.Title      Word WordCount SentimentScore.text SentimentScore.sentiment
1  Placeholder        60 More Placeholders   alright        40             alright                  Neutral
2  Placeholder        60 More Placeholders       bad        79                 bad                 Negative
3  Placeholder        60 More Placeholders       bad       192                 bad                 Negative
4  Placeholder        60 More Placeholders    better       125              better                 Positive
5  Placeholder        60 More Placeholders     brand        24               brand                  Neutral
6  Placeholder        60 More Placeholders     break       106               break                 Negative
7  Placeholder        60 More Placeholders     cause        18               cause                  Neutral
8  Placeholder        60 More Placeholders      come        59                come                  Neutral

此数据集包含来自 xlsx 文件的数据。只有最后一列 Sentiment 是我自己通过使用 RSentimentpackage 添加的。

尝试将其插入 csv 时,我收到以下消息:

> write.csv(dataset,"calculated.csv")
Error in if (inherits(X[[j]], "data.frame") && ncol(xj) > 1L) X[[j]] <- as.matrix(X[[j]]) : 
missing value where TRUE/FALSE needed

经过一番研究,我发现这是因为我的数据集包含一个嵌套数据框,但建议的解决方案 Like This 没有帮助。 (在这种情况下,我收到了 invalid subscript type 'closure' 错误。)

编辑:我看到很多帖子都被问到这个问题:

> str(dataset)
'data.frame':   28 obs. of  6 variables:
 $ Artist        : chr  "Placeholder" "Placeholder" "Placeholder" "Placeholder" ...
 $ Song.Rank     : num  60 60 60 60 60 60 60 60 60 60 ...
 $ Song.Title    : chr  "More Placeholder" "More Placeholder" "More Placeholder" "More Placeholder" ...
 $ Word          : chr  "alright" "bad" "bad" "better" ...
 $ WordCount     : num  40 79 192 125 24 106 18 59 138 146 ...
 $ SentimentScore:'data.frame': 28 obs. of  2 variables:
  ..$ text     : Factor w/ 23 levels "alright","bad",..: 1 2 2 3 4 5 6 7 8 9 ...
  ..$ sentiment: Factor w/ 3 levels "Negative","Neutral",..: 2 1 1 3 2 1 2 2 2 2 ...

【问题讨论】:

  • 您无法将嵌套的 data.frame 保存到 csv。您将需要多个csv。或者你将不得不传播你的内部data.frames。无论哪种方式,如果您想保持嵌套性,似乎 csv 不是去这里的方式..

标签: r


【解决方案1】:

我在这里假设您不想要嵌套的 data.frames。然后做这样的事情:

new_dataset <- data.frame(subset(dataset, select = -c(SentimentScore)), 
                          dataset$SentimentScore)
write.csv(new_dataset, 
          file = "dataset.csv", 
          quote = FALSE)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    • 2018-09-05
    • 2017-11-08
    • 2014-09-26
    相关资源
    最近更新 更多