R:通过某些列名(作为变量)和字段值对数据框进行子集化 [英] R: subsetting data frame by both certain column names (as a variable) and field values
本文介绍了R:通过某些列名(作为变量)和字段值对数据框进行子集化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有名字列表,我有一个数据框,其中的列名有时与列表中的名字相匹配.现在我想根据两个条件对数据框进行子集化:列表中的列名(作为变量)和这些列中字段的值.
I have list of names and I have a data frame with colnames that match sometimes the names in the list. Now I want to subset the data frame based on two criteria: the colnames (as a variable) in the list and the values of the fields in those columns.
我是这样试的:
names.list <- c("name1", "name2" , "name5")
names <- as.data.frame(names.list)
df <- *dataframe with colnames "name1", "name2", "name3", "name4", etc.*
for (i in 1:nrow(names)){
name <- names[i,1]
df <- subset(df, name > 1.5)
}
我知道这是错误的方法,但我还没有想出正确的方法.有人知道怎么做吗?
I know this is the wrong way, but I haven't figured out yet to do it properly. Does anyone know how to do this?
非常感谢!
推荐答案
使用老式的 [
和 [[
运算符:
Use the old-fashioned [
and [[
operators:
name <- "name1"
df[df[[name]] > 1.5, ]
(etc)
这篇关于R:通过某些列名(作为变量)和字段值对数据框进行子集化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文