I have a problem very similar to the one here: Reshape three column data frame to matrix ("long" to "wide" format)
Except I am taking data from a text file, and I'm trying to use the reshape2
library and dcast
method
here is my text file:
'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
it should be changed to this format, because i want to use barplot(as.matrix(data))
on it.
'Group','Illerate','Primary','AtLeastMiddle'
'Shifting',114,10,45
'Settled',76,2,53
'Town',93,13,208
I don't know what to enter for the value.var part of dcast. I'm assuming it's frequency. My current attempts to reshape the data look like this:
> 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