0

Very new to R!

I have a survey with people answering from 0 to 10. I want to add up how many people were <= 6. How many 7 and 8. How many >=9.

I had to turn the questions (Return, Trustworthy...) into a factors to make a ggplots with 1 to 10 on the x axis.

uk_super_q<-read.csv("SUPR_Q_UK.csv", header = TRUE)

uk_super_q.Return <- as.factor(uk_super_q$Return)
uk_super_q.Trustworthy <- as.factor(uk_super_q$Trustworthy)
uk_super_q.Credible <- as.factor(uk_super_q$Credible)
uk_super_q.Trustworthy <- as.factor(uk_super_q$Trustworthy)
uk_super_q.Clean.and.Simple <- as.factor(uk_super_q$Clean.and.Simple)
uk_super_q.Easy.to.use <- as.factor(uk_super_q$Easy.to.use)
uk_super_q.Attractive <- as.factor(uk_super_q$Attractive)
uk_super_q.NPS <- as.factor(uk_super_q$NPS)

uk_super_q$Return <- as.factor(uk_super_q$Return)
ggplot(uk_super_q, aes(x = Return)) +
  geom_bar() +
  xlab("Return") +
  ylab("Total Count") +
  labs(fill = "Blah") 
table(uk_super_q.Return)

uk_super_q$Easy.Nav <- as.factor(uk_super_q$Easy.Nav)
ggplot(uk_super_q, aes(x = Easy.Nav)) +
  geom_bar() +
  xlab("Easy.Nav") +
  ylab("Total Count") +
  labs(fill = "Blah") 
table(uk_super_q.Trustworthy)

uk_super_q$Credible <- as.factor(uk_super_q$Credible)
ggplot(uk_super_q, aes(x = Credible)) +
  geom_bar() +
  xlab("Credible") +
  ylab("Total Count") +
  labs(fill = "Blah") 
table(uk_super_q.Credible)

uk_super_q$Attractive <- as.factor(uk_super_q$Attractive)
ggplot(uk_super_q, aes(x = Attractive)) +
  geom_bar() +
  xlab("Attractive") +
  ylab("Total Count") +
  labs(fill = "Blah") 
table(uk_super_q.Attractive)

uk_super_q$Trustworthy <- as.factor(uk_super_q$Trustworthy)
ggplot(uk_super_q, aes(x = Trustworthy)) +
  geom_bar() +
  xlab("Trustworthy") +
  ylab("Total Count") +
  labs(fill = "Blah") 
table(uk_super_q.Trustworthy)

uk_super_q$Clean.and.Simple <- as.factor(uk_super_q$Clean.and.Simple)
ggplot(uk_super_q, aes(x = Clean.and.Simple)) +
  geom_bar() +
  xlab("Clean.and.Simple") +
  ylab("Total Count") +
  labs(fill = "Blah") 
table(uk_super_q.Clean.and.Simple)

uk_super_q$Easy.to.use <- as.factor(uk_super_q$Easy.to.use)
ggplot(uk_super_q, aes(x = Easy.to.use)) +
  geom_bar() +
  xlab("Easy.to.use") +
  ylab("Total Count") +
  labs(fill = "Blah") 
table(uk_super_q.Easy.to.use)

uk_super_q$NPS <- as.factor(uk_super_q$NPS)
ggplot(uk_super_q, aes(x = NPS)) +
  geom_bar() +
  xlab("NPS") +
  ylab("Total Count") 

table(uk_super_q.NPS)
Community
  • 1
  • 1
Peter Nsanze
  • 69
  • 1
  • 11
  • Hi Peter there looks to be a lot here that does not contribute to your question. Why not start with a small reproducible example that outlines what you have tried, where it failed, and then it should be simple enough to help. Your problem does not seem like a difficult one. One other thing you should do is to post an example of what the data in SUPR_Q_UK.csv looks like, at least a sample so we can test your code with it. Cheers – Alos Jul 08 '16 at 16:33
  • Hi @Alos, thanks I will. I was just trying to show how I got from my data frame to factors and that may be why I cannot do subset functions... – Peter Nsanze Jul 08 '16 at 16:36
  • @PeterNsanze Regardless of what you were trying to do you should always try to keep it minimal. Why do you need to have all of the variables you actually care about in your question? They don't add anything for us. And honestly none of your code adds much to your question. We can't access your data so it's not runnable for us. I suggest reading this: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Dason Jul 08 '16 at 16:39
  • This seemed to work: uk_super_q<-read.csv("SUPR_Q_UK.csv", header = TRUE) Return.Detractors<-subset(uk_super_q, NPS <=6) – Peter Nsanze Jul 08 '16 at 16:40
  • Point taken. I know you can's see my source csv. Just showing the mess I got into. So it seems to work if I do it as above. I believe the problem was I turned NPS into a factor so further down it could not use it for the > calculation. How does one "unfactor" a factor for a brief calucation, please? – Peter Nsanze Jul 08 '16 at 16:44
  • Hi Peter the factor question was answered here: http://stackoverflow.com/questions/3418128/how-to-convert-a-factor-to-an-integer-numeric-without-a-loss-of-information In particular, as.numeric applied to a factor is meaningless, and may happen by implicit coercion. To transform a factor f to approximately its original numeric values, as.numeric(levels(f))[f] is recommended and slightly more efficient than as.numeric(as.character(f)). – Alos Jul 08 '16 at 19:12

1 Answers1

0

Applying logical statements to a data.frame returns a matrix of TRUE/FALSE values, which are coded in R as 1 and 0, respectively. This allows you to count the number of TRUE values in each column with sum, or more efficiently, with colSums.

colSums(uk_super_q <= 6)
colSums(uk_super_q >= 7 & uk_super_q <= 8)
colSums(uk_super_q >= 9)
Alexey Shiklomanov
  • 1,592
  • 13
  • 23
  • Hi! That seemed to work, apart from colSums(uk_super_q %in% 7:8) – Peter Nsanze Jul 08 '16 at 16:49
  • Hmm, OK, I thought there was a chance that one wouldn't work. I'll revise the answer to something that should definitely work if the first and last lines do. – Alexey Shiklomanov Jul 08 '16 at 16:52
  • Worked! (Even thopught it is giving error messages the number match what my manual calculation showed. Thank you!!! :-D Alexey – Peter Nsanze Jul 08 '16 at 17:05
  • Warning messages: 1: In Ops.factor(left, right) : ‘>=’ not meaningful for factors 2: In Ops.factor(left, right) : ‘>=’ not meaningful for factors – Peter Nsanze Jul 08 '16 at 17:07
  • Those are expected, since you're applying a numerical comparison to every column, even though it doesn't make sense for some columns (such as factor columns, as this Warning tells you). If you like the answer, please upvote and accept. Glad everything worked! – Alexey Shiklomanov Jul 08 '16 at 17:09
  • For sure! Very helpful. Now...how do I accept it? I work in UX and this site is failing me ;-) – Peter Nsanze Jul 09 '16 at 19:41
  • To the left of the answer, hit the "up" button (above the zero) to upvote. And directly under the down arrow, there should be a grey check mark that you can hit to accept the answer. – Alexey Shiklomanov Jul 09 '16 at 19:45
  • Done! (But today, the results from yesterday are giving errors for all columns. I haven't changed anything. I just ran it on another machine... – Peter Nsanze Jul 09 '16 at 21:07