dataframe – Extract rows from R data frame based on factors (strings)

dataframe – Extract rows from R data frame based on factors (strings)

Try using the subset function.

This site provides a good reference:
HowtoInR

my_data = subset(my_data, gender == male)

This is an answer to an old question, but Id like to share my current way of doing things where mistakes like this happen a lot less.

The answer is the data.table package. It has saved me hundreds of lines of code and will continue to do so. Subsetting becomes a piece of cake:

my_data <- data.table(my_data)
my_data[gender == male & age <= 20]

I can string as many conditionals as I like, and also use .SD to pass columns as arguments to functions, like so:

my_data[gender == male & age <= 20, lapply(.SD, mean), by = c(nationality, height)]

Column creation from existing columns is much simpler, even creating multiple columns at once

dataframe – Extract rows from R data frame based on factors (strings)

Leave a Reply

Your email address will not be published.