When I try to melt my data frame with mixed data types, I get NA. How can I best solve this?

My purpose and context

I have a data frame in R that I want to melt using the reshape2 library. There are two reasons.

  • I want to make an assessment for each user for each question on the histogram using ggplot.

  • I want to put this data in Excel so that I can see, for each user, their mood, rating and mixing for motivation, relationships before, etc. My intention was to use melt and then cast to put the data in widescreen for easy import into Excel.

My problem

When I try to start melting, I get a warning and end with NA in the resulting molten data frame.

Warning messages:
1: In `[<-.factor`(`*tmp*`, ri, value = c(0.148024, 0.244452, -0.00421,  :
invalid factor level, NAs generated
2: In `[<-.factor`(`*tmp*`, ri, value = c(0L, 0L, 0L, 0L, 0L, 0L, 0L,  :
invalid factor level, NAs generated

. , , .

.

1: R?

2: , ?

.

words <- data.frame(read.delim("sentiments-test-subset-no-text.txt", header=FALSE))
names(words) <- c("level", "question", "user", "sentiment", "score", "mixed")
words$user <- as.factor(words$user)
words.m <- melt(words, id.vars=c("user", "level"), measure.vars=c("sentiment", "score",     "mixed"))

, , , .

, , .

experimental    motivated   1   positive    0.148024    0
experimental    motivated   2   positive    0.244452    0
experimental    motivated   3   negative       -0.004210    0
experimental    motivated   4   unknown         0.000000    0
experimental    attitudeBefore  1   negative       -0.241500    0
experimental    attitudeBefore  2   neutral         0.000000    0
experimental    attitudeBefore  3   neutral         0.000000    0
experimental    attitudeBefore  4   unknown         0.000000    0

dput dump

dput .

structure(list(level = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L), .Label = "experimental", class = "factor"), question = structure(c(2L, 
2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("attitudeBefore", "motivated"
), class = "factor"), user = structure(c(1L, 2L, 3L, 4L, 1L, 
2L, 3L, 4L), .Label = c("1", "2", "3", "4"), class = "factor"), 
sentiment = structure(c(3L, 3L, 1L, 4L, 1L, 2L, 2L, 4L), .Label = c("negative", 
"neutral", "positive", "unknown"), class = "factor"), score = c(0.148024, 
0.244452, -0.00421, 0, -0.2415, 0, 0, 0), mixed = c(0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("level", "question", 
"user", "sentiment", "score", "mixed"), row.names = c(NA, -8L
), class = "data.frame")
+5
1

, . reshape reshape2 - .

library(reshape2)
words.m <- melt(words, id.vars=c("user", "level"), measure.vars=c("sentiment", "score",     "mixed"))
# no problem

detach(package:reshape2)

# using reshape instead of reshape2
library(reshape)
words.m <- melt(words, id.vars=c("user", "level"), measure.vars=c("sentiment", "score",     "mixed"))
# Warning messages:
# 1: In `[<-.factor`(`*tmp*`, ri, value = c(3L, 3L, 1L, 4L, 1L, 2L, 2L,  :
#   invalid factor level, NAs generated
# 2: In `[<-.factor`(`*tmp*`, ri, value = c(3L, 3L, 1L, 4L, 1L, 2L, 2L,  :
#   invalid factor level, NAs generated

reshape2 , CRAN

 install.packages("reshape2")
+4
source

All Articles