Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

dataframe

r

Sorry if this is a duplicate, but I can't seem to find the information anywhere else on SO, even though it seems like such a simple problem. I have a data frame with several columns as factors. Some of those are integers, and some are strings. I would like to extract the rows that correspond to a particular factor. For example,

my_data <- read.table(file = "my_data.txt", header = TRUE)
my_data[ my_data$age == 20, ]

This works, but if I then try

my_data[ my_data$gender == "male", ]

This produces no matches. I realized they are not the same thing, as checking the class of my_data$name[1] gives factor, while I'm checking it against a string.

Any ideas what I'm doing wrong here?

Cheers

Data sample: Size Age Gender Value 1 20 male 0.5 4 22 female 0.7 3 14 female 0.3

like image 506
Samuel Tan Avatar asked Feb 12 '14 01:02

Samuel Tan


People also ask

How do you select rows from a DataFrame based on column values in R?

Select Rows by list of Column Values. By using the same notation you can also use an operator %in% to select the DataFrame rows based on a list of values. The following example returns all rows when state values are present in vector values c('CA','AZ','PH') .


1 Answers

Try using the subset function.

This site provides a good reference: HowtoInR

my_data = subset(my_data, gender == "male")
like image 68
LearnR Avatar answered Nov 15 '22 02:11

LearnR