简体   繁体   English

R按来自另一个数据框的列中的值对数据框进行子集,并为每个子集命名

[英]R Subset data frame by values in column from another data frame and give name to each subset

I want to subset a data frame (with millions of data rows) thousands of times, using values in two columns in another data frame.我想使用另一个数据框中两列中的值将数据框(具有数百万数据行)子集数千次。 Currently I was using the example provided by Akrun目前我正在使用 Akrun 提供的示例

     subset(df1, (Latitude >= (df2$Lat - 0.01)) & (Latitude <= (df2$Lat + 0.01)))

However, this seems to return all of the data that matches any of the rows in the second data frame.但是,这似乎返回了与第二个数据框中的任何行匹配的所有数据。 How can I adjust this so that it takes a third column from the second data frame as a name for each row subset pair?我该如何调整它,以便将第二个数据框中的第三列作为每个行子集对的名称?

Reference;参考; Subset data frame based on range of values in second data frame 基于第二个数据帧中的值范围的子数据帧

# Subsetted data
df_sub <- subset(df1, (Latitude >= (df2$Lat - 0.01)) & (Latitude <= (df2$Lat + 0.01)))
# Names of third column
towns <- df2$Town[(df1$Latitude >= (df2$Lat - 0.01)) & (df1$Latitude <= (df2$Lat + 0.01))]

df_out <- cbind(df_sub, towns)

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

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