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
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') .
Try using the subset
function.
This site provides a good reference: HowtoInR
my_data = subset(my_data, gender == "male")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With