简体   繁体   English

在值列表之后,我想对 r 中的数据框进行子集化,其中行包含某列中的值

[英]Following a list of values, I want to subset a data frame in r with rows containing the values in a certain column

I have a data frame and want to extract the rows that the value coincide with a certain vector.我有一个数据框,想提取值与某个向量一致的行。 In the df, there are no duplicate.在df中,没有重复。 I need the data frame to keep the order of the vector.我需要数据框来保持向量的顺序。

Name <- c("Jon", "Bill", "Maria", "Ben", "Tina")
Age <- c(23, 41, 32, 58, 26)

mydf <- data.frame(Name, Age)

myvector <- c(23, 26, 32, 26) 

My expected answer would be我的预期答案是

"Jon", "Tina", "Maria", "Tina"

This is one of the things I've tried:这是我尝试过的事情之一:

> df[df$Age == to_find,]$Name
Warning message:
In df$Age == to_find :
  longer object length is not a multiple of shorter object length

Also this next solution doesn't give me all the expected rows and still gives me an error message:此外,下一个解决方案并没有给我所有预期的行,并且仍然给我一条错误消息:

> subset(df, Age == to_find)
   Name Age
1   Jon  23
3 Maria  32
Warning message:
In Age == to_find :
  longer object length is not a multiple of shorter object length

Thank you for your help!谢谢您的帮助!

The for loop below returns your desired output.下面的 for 循环返回您想要的 output。 It is also based on the assumption that no two people in your original dataset will have the same age.它还基于原始数据集中没有两个人的年龄相同的假设。 If that is the case, it simply gets the name of the first person in your dataset who matches the age.如果是这种情况,它只会获取数据集中第一个与年龄匹配的人的姓名。 For example, if 'myvector' is looking for people aged 34, and there are two people, Joseph and Brian aged 34 in mydf, the new 'names' vector will only get the name of the first person from the dataset who matches the age 34, regardless of how many times you look for 34.例如,如果 'myvector' 正在寻找 34 岁的人,并且 mydf 中有两个人,即 34 岁的 Joseph 和 Brian,则新的 'names' 向量将仅从数据集中获取与年龄匹配的第一个人的姓名34,不管你找了多少次34。

Name <- c("Jon", "Bill", "Maria", "Ben", "Tina")
Age <- c(23, 41, 32, 58, 26)

mydf <- data.frame(Name, Age)

myvector <- c(23, 26, 32, 26)

names <- vector(mode="character", length=length(myvector))

for (i in 1:length(names)) {
  
  for (j in 1:length(mydf$Name)) {
    
    if(mydf$Age[j] == myvector[i]) {
      names[i] <- mydf$Name[j]
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 数据框选定列中包含 NA(缺失)值的行子集 - Subset of rows containing NA (missing) values in a chosen column of a data frame R。 我正在尝试将我的数据框子集几十年。 因此,我想通过使用列的值进行子集化 - R. I am trying to subset my data frame by decades. Therefore I want to subset by using values of a column 基于包含值因子的列值的子集数据框 - Subset data frame based on column values containing factors of a value 将某些值的行与聚合组合,然后将聚合子集返回到数据框 - Combining rows of certain values with aggregate, and then returning aggregate subset to data frame R-按列值列表删除数据框中的行 - R - Removing rows in data frame by list of column values 根据列 x 的值对数据框进行子集化。 只想要R中的前两位 - Subset a data frame based on count of values of column x. Want only the top two in R 我想从R数据框中的一列生成5个名称的组合,其在不同列中的值加起来等于或小于一定数量 - I want to generate combinations of 5 names from a column in an R data frame, whose values in a different column add up to a certain number or less 在data.frame中选择行,其中某个列的值包含一组前缀之一 - selecting rows in a data.frame in which a certain column has values containing one of a set of prefixes 如何为数据帧行的子集的列分配值 - How to assign values to a column for a subset of data frame rows 基于数据框 R 子集的一列中“分类值”的百分比 - Percentages of "categorical values" in one column based on subset of data frame R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM