R - 写入csv时出错

时间:2017-01-26 14:34:44

标签: r

我有以下数据集:

 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是我使用RSentiment包添加自己的内容。

尝试将其插入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 ...

1 个答案:

答案 0 :(得分:2)

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

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