1

I have following dataset:

 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

This dataset exists of data from an xlsx file. Only the last column Sentiment is something I added myself by using the RSentimentpackage.

When trying to insert it into a csv, I get the following message:

> 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

After a bit of research, I found out that this happens because my dataset contains a nested dataframe, but the suggested solutions Like This didn't help. (in this case, I got a invalid subscript type 'closure' error.)

EDIT: I saw a lot of posts where this was asked:

> 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 ...
Community
  • 1
  • 1
DenStudent
  • 906
  • 1
  • 13
  • 37
  • You cannot save a nested data.frame to a csv. You would need multiple csv's. Or you would have to spread your internal data.frames. Either way it seems like csv is not the way to go here if you want to keep the nestedness.. – Dr. Mike Jan 26 '17 at 14:49

1 Answers1

2

I am assuming here you don't want nested data.frames. Then just do something like this:

new_dataset <- data.frame(subset(dataset, select = -c(SentimentScore)), 
                          dataset$SentimentScore)
write.csv(new_dataset, 
          file = "dataset.csv", 
          quote = FALSE)
tstev
  • 607
  • 1
  • 10
  • 20