Reputation: 174
I have a data set with numerical responses to several questions. I would like to know the number of times a person answers a question with a value of 1,2...
Here is an example of the data:
df=data.frame("Person"=c("person a", "person b"),
"Q1"=c(2,2),"Q2"=c(1,2),"Q3"=c(1,1))
Which looks like this:
Person Q1 Q2 Q3
person a 2 1 1
person b 2 2 1
I would like this and would prefer to use dplyr:
Person Q1 Q2 Q3 Total.1 Total.2
person a 2 1 1 2 1
person b 2 2 1 1 2
Upvotes: 1
Views: 137
Reputation: 887881
Here is one option with tidyverse
library(tidyverse)
df %>%
mutate(Total = pmap(.[-1], ~
c(...) %>%
paste0("Total.", .) %>%
table %>%
as.list %>%
as_tibble )) %>%
# unnest
# Person Q1 Q2 Q3 Total.1 Total.2
#1 person a 2 1 1 2 1
#2 person b 2 2 1 1 2
Or another way is
df %>%
mutate(Total = pmap(.[-1], ~
c(...) %>%
table %>%
toString)) %>%
separate(Total, into = c("Total.1", "Total.2"))
# Person Q1 Q2 Q3 Total.1 Total.2
#1 person a 2 1 1 2 1
#2 person b 2 2 1 1 2
Upvotes: 0
Reputation: 389265
The base R approach suggested by @dww is quite simple and straight forward. However, if you prefer dplyr
approach we can use rowwise
and do
to calculate occurrence of 1 and 2 respectively.
library(dplyr)
df %>%
rowwise() %>%
do( (.) %>% as.data.frame %>%
mutate(Total.1 = sum(.==1),
Total.2 = sum(.==2)))
# Person Q1 Q2 Q3 Total.1 Total.2
# <fct> <dbl> <dbl> <dbl> <int> <int>
#1 person a 2 1 1 2 1
#2 person b 2 2 1 1 2
A base R approach using apply
df[c("Total.1", "Total.2")] <- t(apply(df, 1, function(x) c(sum(x==1), sum(x==2))))
df
# Person Q1 Q2 Q3 Total.1 Total.2
#1 person a 2 1 1 2 1
#2 person b 2 2 1 1 2
Upvotes: 2
Reputation: 31454
No need for dplyr. In base R it is quite simple
df = cbind(df, Total.1 = rowSums(df[,-1]==1), Total.2 = rowSums(df[,-1]==2))
Upvotes: 2