Create columns from factors and count
You only need to make some slight modification to your code. You should use .(Name)
instead of c("Name")
:
ddply(df1, .(Name), summarise,
Score_1 = sum(Score == 1),
Score_2 = sum(Score == 2),
Score_3 = sum(Score == 3))
gives:
Name Score_1 Score_2 Score_3
1 Ben 1 1 0
2 John 1 1 1
Other possibilities include:
1. table(df1)
as @alexis_laz mentioned in the comments, this gives:
> table(df1)
Score
Name 1 2 3
Ben 1 1 0
John 1 1 1
2. The dcast
function of the reshape2 package (or data.table which has the same dcast
function):
library(reshape2) # or library(data.table)
dcast(df1, Name ~ paste0("Score_", Score), fun.aggregate = length)
gives:
Name Score_1 Score_2 Score_3
1 Ben 1 1 0
2 John 1 1 1
Count occurrences of factors across multiple columns in grouped dataframe
You can stack col1
& col2
together, count the number of each combination, and then transform the table to a wide form.
library(dplyr)
library(tidyr)
df %>%
pivot_longer(col1:col2) %>%
count(grp, name, value) %>%
pivot_wider(grp, names_from = c(name, value), names_sort = TRUE,
values_from = n, values_fill = 0)
# A tibble: 3 x 6
grp col1_A col1_B col2_B col2_C col2_D
<chr> <int> <int> <int> <int> <int>
1 a 1 2 2 0 1
2 b 2 0 0 2 0
3 c 1 2 0 2 1
A base
solution (Thank @GKi to refine the code):
table(cbind(df["grp"], col=do.call(paste0, stack(df[-1])[2:1])))
col
grp col1A col1B col2B col2C col2D
a 1 2 2 0 1
b 2 0 0 2 0
c 1 2 0 2 1
R Creating new columns based on factors in another column
Try this
df %>% group_by(category) %>% mutate(id = row_number()) %>% ungroup() %>% pivot_wider(id_cols = id, names_from = category, values_from = cond) %>% select(-id)
Counting factor instance of several columns of factors
We can use rowSums
on a logical matrix
DF$CountYes <- rowSums(DF == 'Yes')
DF$CountYes
#[1] 3 2 0
Counting the number of factor variables in a data frame
A few problems here:
Your subscript is out of bounds problem
is because df[1:5, ]
is rows 1:5, whereas columns would be df[ ,1:5]
. It appears that you only have 3 rows, not 5.
The second error no applicable method for 'as.quoted' applied to
is referring to the as.factor, which is a function. It is saying that a function doesn't belong within the function
an object of class "function"count
. You can check exactly what count
wants by running ?count
in the console
A third problem that I see is that R will not automatically think that integers are factors. You will have to specify this with numbers. If you read in words, they are often automatically set as factors.
Here is a reproducible example:
> df<-data.frame("var1"=rnorm(3),"var2"=c(1:3),"var3"=rnorm(3),"var4"=c(3,1,2),"var5"=rnorm(3))
> str(df)
'data.frame': 3 obs. of 5 variables:
$ var1: num 0.716 1.43 -0.726
$ var2: int 1 2 3
$ var3: num 0.238 -0.658 0.492
$ var4: num 3 1 2
$ var5: num 1.71 1.54 1.05
Here I used the structure str()
function to check what type of data I have. Note, var1
is read in as an integer when I generated it as c(1:3)
, whereas specifying c(3,1,2)
was read in as numeric in var4
Here, I will tell R I want two of the columns to be factors, and I will make another column of words, which will automatically become factors.
> df<-data.frame("var1"=rnorm(3),"var2"=as.factor(c(1:3)),"var3"=rnorm(3),"var4"=as.factor(c(3,1,2))
+ ,"var5"=rnorm(3), "var6"=c("Green","Red","Blue"))
> str(df)
'data.frame': 3 obs. of 6 variables:
$ var1: num -1.18 1.26 -0.53
$ var2: Factor w/ 3 levels "1","2","3": 1 2 3
$ var3: num 1.38 -0.401 -0.924
$ var4: Factor w/ 3 levels "1","2","3": 3 1 2
$ var5: num 1.688 0.547 0.727
$ var6: Factor w/ 3 levels "Blue","Green",..: 2 3 1
You can then as which are factors:
> sapply(df, is.factor)
var1 var2 var3 var4 var5 var6
FALSE TRUE FALSE TRUE FALSE TRUE
And if you wanted a number for how many are factors something like this would get you there:
> length(which(sapply(df, is.factor)==TRUE))
[1] 3
You have something similar: length(which(vec==as.factor))
, but one problem with this is you are asking which things in the vec
object are the same as a function as.factor
, which doesn't make sense. So it is giving you the error Error in vec == as.factor :
comparison (1) is possible only for atomic and list types
as.factor
is for setting things as factor (as I have shown above), but is.factor
is for asking if something is a factor, which will return a logical (TRUE vs FALSE) - also shown above.
Calculate factors across columns in R
Use levels=
when creating the factors. DF
has character columns whereas DF2
has factor columns all having the same levels, levs
.
# test data frame
DF <- as.data.frame(matrix(letters,, 2), stringsAsFactors = FALSE)
DF2 <- DF
levs <- sort(unique(unlist(DF)))
DF2[] <- lapply(DF2, factor, levels = levs)
This could be written as a one-liner like this:
DF2 <- replace(DF, TRUE, lapply(DF, factor, levels = sort(unique(unlist(DF)))))
Creating a table of counts by some factor level in R
library(reshape2)
dcast(data = all, formula = food~day, fun.aggregate = length, value.var = "food")
food 1 2 3
1 pizza 2 0 2
2 snake 1 1 1
3 taco 1 2 2
OR
table(all$food, all$day)
1 2 3
pizza 2 0 2
snake 1 1 1
taco 1 2 2
Related Topics
Generate Paired Stacked Bar Charts in Ggplot (Using Position_Dodge Only on Some Variables)
Issue When Importing Dataset: 'Error in Scan(...): Line 1 Did Not Have 145 Elements'
How to Multiply Data Frame by Vector
Get All Diagonal Vectors from Matrix
Merge Rows in a Dataframe Where the Rows Are Disjoint and Contain Nas
Command Lines Error in Rstudio Console
Global Variables in Packages in R
Replacing Occurrences of a Number in Multiple Columns of Data Frame with Another Value in R
Duplicating (And Modifying) Discrete Axis in Ggplot2
Display Weighted Mean by Group in the Data.Frame
How to Generate Distributions Given, Mean, Sd, Skew and Kurtosis in R
Extract Names of Objects from List
Show Frequencies Along with Barplot in Ggplot2
Create Zip File: Error Running Command " " Had Status 127