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

Sorry if this is a duplicate, but I cannot find the information elsewhere on SO, although this seems like such a simple problem. I have a data frame with several columns as factors. Some of them are integers, and some are strings. I would like to extract rows corresponding to a specific coefficient. For example,

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

It works, but if I then try

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

This does not match. I realized that this is not the same thing, since checking the class my_data$name[1]gives a coefficient, while I check it against a string.

Any ideas what I'm doing wrong here?

Greetings

Sample data: Age Age 1 20 men 0.5 4 22 women 0.7 3 14 women 0.3

+4
2

subset.

: HowtoInR

my_data = subset(my_data, gender == "male")
+7

, , , , .

- data.table. . :

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

, , .SD , :

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

,

+2

Source: https://habr.com/ru/post/1526459/


All Articles