简体   繁体   English

如何根据列表中的值对数据框进行子集化?

[英]How do you subset a dataframe based on values from a list?

I want to return a new dataframe only including those rows that exist in aa new dataframe I created.我想返回一个新的数据帧,只包括我创建的新数据帧中存在的那些行。 When indexing would you do something like %in% or can you have a dataframe or convert into vector inside the brackets?索引时,您会执行 %in% 之类的操作,还是可以在括号内使用数据框或转换为向量?

It would help to get a better info on your problem with an example, but there are a few ways to extract a data frame from specific rows.这将有助于通过示例获得有关您的问题的更好信息,但有几种方法可以从特定行中提取数据框。

One basic way is: new.data <- data[0:100, ] <-This method only takes the rows you specify, in this case the first 100 rows, but it is based on the position of the rows and not their value.一种基本方法是: new.data <- data[0:100, ] <-此方法仅采用您指定的行,在本例中为前 100 行,但它基于行的位置而不是它们的值.

Another way is using dplyr::filter , if you want to subtract rows based on their value.另一种方法是使用dplyr::filter ,如果您想根据行的值减去行。 For example:例如:

data$variableX=c(1,2,3,4,5,6,7)
new.data <- filter(data, variableX==c(1,2,3)) 

Read up on the dplyr filter function for more info and good luck阅读dplyr filter功能以获取更多信息dplyr filter你好运

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM