user2665710
user2665710

Reputation: 1

Filter function throws error in data frame

I have a data frame that includes factors. For one of the factor levels (etoh2), I'd like to count the number of occurrences of the word "never" and divide that by the total number (excluding na) to calc. a percentage.

The structure of the df looks like this: structure of df

And the levels of etoh2 look like this (using sapply):etoh levels

Now, I try to filter using dplyr:

vipcls$etoh2 %>%
never <- count(filter(etoh2 == "never")
all <- count(etoh2)
ratio <- never/all

Which give me the following error message:error message

Any help is greatly appreciated!

Upvotes: 0

Views: 52

Answers (2)

Md Pervez Nawaz
Md Pervez Nawaz

Reputation: 37

You can try this, Ben has explained it better.

library(dplyr)

etoh1 <- c("Hello,", "how", "are", "you", "today", "!", "Hello,", "how", "are", "you", "today", "!")`enter code here`
etoh2 <- c("every day", "3-5/week", "1/week", "</1week", "<1/month", "never", "every day", "3-5/week", "1/week", "</1week", "<1/month", "never")

vipcls <- data.frame(cbind(etoh1, etoh2))
never <- count(vipcls %>% 
                 filter(etoh2 == "never"))
all <- nrow(vipcls) #assuming total rows = total etoh2
ratio <- never/all

ratio

Upvotes: 0

Ben Norris
Ben Norris

Reputation: 5747

Let's figure this out.

You have your assignment operator in the wrong place for starts. The pipe %>% takes the output of one operation and makes it the first argument of the next. You are trying to do

vipcls$etoh2 %>%
  never # This is not a function

Here is the correct dplyr way.

vipcls %>%
  filter(etoh2 == "never") %>% # makes your filtered set
  count() # returns the number of records

To assign this, you put your assignment at the front:

never <- vipcls %>%
  filter(etoh2 == "never") %>% # makes your filtered set
  count() # returns the number of records

You can get all more simply:

all <- nrow(vipcls) #or
all <- count(vipcls)

Your code count(etoh2) will not work because etoh2 is not its own object. It is part of the vicpls object.

Upvotes: 1

Related Questions