r语言 - 当我尝试用混合数据类型熔化我的数据帧时,我得到NAs.我怎样才能最好地解决这个问题?



我的目标和背景

我在R中有一个数据框架,我想使用reshape2库熔化。有两个原因。

  1. 我想使用ggplot在条形图中绘制每个用户对每个问题的得分。

  2. 我想把这些数据放入Excel中,这样我就可以看到,每个用户,他们的情绪,得分,动机,态度等混合。我的意图是使用melt,然后cast将数据转换为宽格式,以便于Excel导入。

<标题> 我的问题

当我尝试运行melt时,我得到一个警告,并最终在我得到的熔融数据帧中得到NAs。

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

我最终在我的结果融化的数据框架中有大量的NAs。我想这是因为我在同一列中同时使用了字符和数字。

<标题>我的问题

结果我有两个问题。

问题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转储h1>

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")

看起来您可能只是使用了错误的库。reshapereshape2不是一回事。

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")

最新更新