Reputation: 43
I have survey, where people sort animals by their popularity. for example:
"first","second","third"
"dog","cat","rabbit"
"cat","rabbit","dog"
"dog","cat","rabbit"
"dog","cat","rabbit"
I want graph, which shows order. So animals in first column get weight 3, in second weight 2 and in third weight 1.
When i do:
data<-read.csv("animals.csv", header=T)
f = summary(data$first)
s = summary(data$second)
t = summary(data$third)
In f is array with:
dog cat
3 1
In s is array with:
cat rabbit
3 1
In t is array with:
dog rabbit
1 3
And i obviously cannot do:
graph = 3*f + 2*s + t
Because they dont have the same column. Exist some function how to do it. How to make weighted combination of these three columns?
In the end i need:
dog cat rabbit
10 9 5
I am a begginer in R, so thank for help.
Upvotes: 0
Views: 930
Reputation: 24074
To get the weighted numbers, you can do:
table(c(rep(data$first, 3), rep(data$second, 2), data$third))
# cat dog rabbit
# 9 10 5
data
data <- structure(list(first = c("dog", "cat", "dog", "dog"), second = c("cat",
"rabbit", "cat", "cat"), third = c("rabbit", "dog", "rabbit",
"rabbit")), .Names = c("first", "second", "third"), class = "data.frame", row.names = c(NA,
-4L))
NB: if the data are imported with characters as factors, you can do:
table(c(rep(as.character(data$first), 3), rep(as.character(data$second), 2), as.character(data$third)))
Upvotes: 3