Home > front end >  How to count occurrences of each potential variable and find their proportions in R?
How to count occurrences of each potential variable and find their proportions in R?

Time:09-24

Ultimately, I am trying to find the proportions of each potential variable in each subgroup.

I have a large data frame (Patient1) with 108.720 rows that includes:

  • factor variable Majority_SNP (Could be A, T, C, G, or dash)
  • Index location (1 to 9.060)
  • many, many other things

I want to eventually do a chart showing the relative frequencies of A, T, C, G, and dashes are the "Majority_SNP" by index location.

I have tried:

Pt1_Majority_SNP_Counts_by_Loci <-
 Patient1 %>%
 group_by(Index) %>%
 table(Majority_SNP)

but I get the following error:

"Error in table(., Majority_SNP) : object 'Majority_SNP' not found"

When I do

table(Patient1$Majority_SNP)

it works fine.

CodePudding user response:

This code sends the data.frame to table, but table works with a vector

 Patient1 %>%
 table(Majority_SNP)

Another approach would be to use the operator $ from magrittr

 Patient1 %$%
 table(Majority_SNP)

Or with from R Base

with(Patient1 , table(Majority_SNP))

CodePudding user response:

If we need to use table on the column outside the dplyr function, pull the column

library(dplyr)
Patient1 %>%
    pull(Majority_SNP) %>%
    table

Or with .$

Patient1 %>%
    table(.$Majority_SNP)
  •  Tags:  
  • r
  • Related