I need to count the number of occurrences of specific values in each column, and then do a for loop for that to run that count() function for the entire dataframe (consisting of several thousand columns).
For instance, if I have a column consisting of: [0,0,0,1,1,0,0,0,0,0,0,0]. I want it to count the column and return for me the information of: 1 -> 2 counts 0 -> 10 counts The dataframe that I have consists entirely of only 0s and 1s. I just need to count how many of them are in each column, but that dataframe has over a few thousand columns.
Currently, my for loop code doesnt work, it seems to only register the first column and keep printing that same first column result over and over again. Thanks everyone!!
s <- 0
yes_filt_high_mutation <- data.frame();
for(c in colnames(high_mutations)[2:ncol(high_mutations)]){ #high_mutations = my dataframe
mutation_results = high_mutations %>% count(high_mutations$c); #Count the # of 0s and 1s in each column
print(c)
print(mutation_results)
s <- s + 1
add_column <- c(c,mutation_results[1,2],mutation_results[2,2])
yes_filt_high_mutation <- rbind(data.frame(yes_filt_high_mutation), add_column)
}
names(yes_filt_high_mutation)[1] <- "Samples"
names(yes_filt_high_mutation)[2] <- "Number of 0's"
names(yes_filt_high_mutation)[3] <- "Number of 1's"
I want my result to be something like this, for each loop result: So essentially tell me that there are 134 counts of 0 and 2 counts of 1 in Column 1.
high_mutations$Column1 n
1 0 134
2 1 2