【发布时间】:2015-09-07 18:51:53
【问题描述】:
我有一个与这里非常相似的问题:Reshape three column data frame to matrix ("long" to "wide" format)
除非我从文本文件中获取数据,并且我正在尝试使用 reshape2 库和 dcast 方法
这是我的文本文件:
'Group','LiteracyLevel','Frequency'
'Shifting','Illerate',114
'Shifting','Primary',10
'Shifting','AtLeastMiddle',45
'Settled','Illerate',76
'Settled','Primary',2
'Settled','AtLeastMiddle',53
'Town','Illerate',93
'Town','Primary',13
'Town','AtLeastMiddle',208
应该改成这种格式,因为我想在上面使用barplot(as.matrix(data))。
'Group','Illerate','Primary','AtLeastMiddle'
'Shifting',114,10,45
'Settled',76,2,53
'Town',93,13,208
我不知道为 dcast 的 value.var 部分输入什么。我假设它的频率。我目前重塑数据的尝试如下所示:
> data <- read.csv("ex3-39.txt", header=TRUE)
> dcast(data, data$Group~data$LiteracyLevel, value.var="X.Frequency")
Error: value.var (X.Frequency) not found in input
> dcast(data, data$Group~data$LiteracyLevel, value.var="Frequency")
Error: value.var (Frequency) not found in input
> dcast(data, data$Group~data$LiteracyLevel, value.var="data$X.Frequency")
Error: value.var (data$X.Frequency) not found in input
> dcast(data, data$Group~data$LiteracyLevel, value.var=data$X.Frequency)
Error: value.var (1141045762539313208) not found in input
In addition: Warning message:
In if (!(value.var %in% names(data))) { :
the condition has length > 1 and only the first element will be used
> dcast(data, data$Group~data$LiteracyLevel, value.var=Frequency)
Error in match(x, table, nomatch = 0L) : object 'Frequency' not found
【问题讨论】:
-
也许您的数据看起来不像您期望的那样(请提供
dput(data)的输出)如果您在 dcast 中有一个数据框,您可以在您的打电话给decast。dcast(data, Group~LiteracyLevel, value.var="Frequency")
标签: r matrix dataframe reshape2