Dataframe not populating when I am doing subset?

3997 views r

I have a dataframe that has two columns. One column is the product type, and the other is characters. I essentially want to break that column 'product' into 12 different data frames for each level. So for the first level, I am running this code:

df = df %>% select('product','comments') 
df['product'] = as.character(df['product']) 
df['comments'] = as.character(df['comments'])

Now that the dataframe is in the structure I want it, I want to take a variety of subsets, and here is my first subset code:

df_boatstone = df[df$product == 'water',]
#df_boatstone <- subset(df, product == "boatstone", select = c('product','comments'))

I have tried both methods, and the dataframe is being created, but has nothing in it. Can anyone catch my mistake?

answered question

1 Answer


The as.character works on a vector, while df['product'] or df['comments'] are both data.frame with a single column

df[['product']] <- as.character(df[['product']])

Or better would be

df %>% 
    select(product, comments) %>%
    mutate_all(as.character) %>%
    filter(product == 'water')

posted this

Have an answer?


Please login first before posting an answer.