我有一个包含3个观测值的数据集,分别是姓名,年龄,学校
Name | Age | School
John 25 STM |
Anne 20 CODC |
clark 25 GCC |
Tim 25 BBC |
Mary 15 CBIC
如何提取年龄分别为25岁和20岁的姓名
答案 0 :(得分:0)
我建议您安装tidyverse软件包并从现在开始学习如何使用它们。用dplyr表示法,您需要的内容如下所示:
library(dplyr)
df <- data_frame(
Name = c("John", "Anne", "Clark", "Tim", "Mary"),
Age = c(25, 20, 25, 25, 15),
School = c("STM", "CODC", "GCC", "BBC", "CBIC")
)
desired_names <- df %>%
filter(Age %in% c(25,15) %>%
select(Name) # use pull(Name) instead if you want the names as a vector