如何将多个数据帧作为边界框进行子集化?

时间:2022-02-17 19:36:33

I have three dataframes.

我有三个数据帧。

a) Latitude:

a)纬度:

20.08824    20.11288    20.13752    20.16218
20.09489    20.11954    20.14417    20.16882
20.11476    20.13941    20.16408    20.18874
20.12138    20.14604    20.17071    20.19536

b) Longitude:

b)经度:

-118.0981   -118.1052   -118.1123   -118.1194
-117.9931   -118.0002   -118.0072   -118.0143
-117.8092   -117.8162   -117.8232   -117.8302
-117.783    -117.7899   -117.7969   -117.8039

c) A variable, let's say windspeed

c)一个变量,比如说windspeed

2   2   3   4
2   4   3   3
4   4   5   9
6   4   2   5

I have the Lat-Long coordinates for the edges of a bounding box for which I want to subset (c)

我有一个Lat-Long坐标,用于我想要子集的边界框的边缘(c)

20.14417,-118.0002
20.14417,-118.0072
20.11954,-118.0002
20.11954,-118.0072

[Bounding Box][http://www.darrinward.com/lat-long/?id=634088]

I know how to subset columns within one dataframe, but I don't know how to do it across three dataframes. Could you please help.

我知道如何在一个数据帧中对列进行子集,但我不知道如何在三个数据帧中进行。能否请你帮忙。

Note: (a), (b) and (c) are Global datasets with exactly same dimensions. The data given here is only illustrative.

注意:(a),(b)和(c)是具有完全相同尺寸的全局数据集。这里给出的数据仅是说明性的。

ADDENDUM

附录

I have large Matrix, not dataframes, if it makes a difference.

我有大型Matrix,而不是数据帧,如果它有所作为。

2 个解决方案

#1


1  

First, I would put everything into a data frame:

首先,我会将所有内容放入数据框中:

x<-data.frame(as.vector(lat),as.vector(long),as.vector(wind))
names(x)<-c("lat","long","wind")
dput(x)

giving:

赠送:

 structure(list(lat = c(20.08824, 20.09489, 20.11476, 
20.12138, 20.11288, 20.11954, 20.13941, 20.14604, 20.13752, 20.14417, 
20.16408, 20.17071, 20.16218, 20.16882, 20.18874, 20.19536), 
    long = c(-118.0981, -117.9931, -117.8092, -117.783, 
    -118.1052, -118.0002, -117.8162, -117.7899, -118.1123, -118.0072, 
    -117.8232, -117.7969, -118.1194, -118.0143, -117.8302, -117.8039
    ), wind = c(2L, 2L, 4L, 6L, 2L, 4L, 4L, 4L, 3L, 3L, 
    5L, 2L, 4L, 3L, 9L, 5L)), .Names = c("lat", "long", 
"wind"), row.names = c("V11", "V12", "V13", "V14", "V21", 
"V22", "V23", "V24", "V31", "V32", "V33", "V34", "V41", "V42", 
"V43", "V44"), class = "data.frame")

Then it's a simple subset:

然后它是一个简单的子集:

x[x$lat < 20.14417 & x$lat > 20.11954 & x$long < -118.0002 & x$long > -118.0072,]

#2


0  

You can subset across data frames by placing them into other other subsettable objects. Arrays and lists are used to hold them.

您可以通过将数据框放入其他其他可子集化对象来对数据框进行分组。数组和列表用于保存它们。

lst <- list(df1, df2, df3)

Then you can subset with functions. For example, the first column of each data frame:

然后你可以用函数进行子集化。例如,每个数据框的第一列:

lapply(lst, `[`, 1)
[[1]]
        V1
1 20.08824
2 20.09489
3 20.11476
4 20.12138

[[2]]
         V1
1 -118.0981
2 -117.9931
3 -117.8092
4 -117.7830

[[3]]
  V1
1  2
2  2
3  4
4  6

The same first column subset can be done with an array:

可以使用数组完成相同的第一列子集:

a <- simplify2array(c(df1, df2, df3))
dim(a) <- c(4,4,3)
a[,1,]

#1


1  

First, I would put everything into a data frame:

首先,我会将所有内容放入数据框中:

x<-data.frame(as.vector(lat),as.vector(long),as.vector(wind))
names(x)<-c("lat","long","wind")
dput(x)

giving:

赠送:

 structure(list(lat = c(20.08824, 20.09489, 20.11476, 
20.12138, 20.11288, 20.11954, 20.13941, 20.14604, 20.13752, 20.14417, 
20.16408, 20.17071, 20.16218, 20.16882, 20.18874, 20.19536), 
    long = c(-118.0981, -117.9931, -117.8092, -117.783, 
    -118.1052, -118.0002, -117.8162, -117.7899, -118.1123, -118.0072, 
    -117.8232, -117.7969, -118.1194, -118.0143, -117.8302, -117.8039
    ), wind = c(2L, 2L, 4L, 6L, 2L, 4L, 4L, 4L, 3L, 3L, 
    5L, 2L, 4L, 3L, 9L, 5L)), .Names = c("lat", "long", 
"wind"), row.names = c("V11", "V12", "V13", "V14", "V21", 
"V22", "V23", "V24", "V31", "V32", "V33", "V34", "V41", "V42", 
"V43", "V44"), class = "data.frame")

Then it's a simple subset:

然后它是一个简单的子集:

x[x$lat < 20.14417 & x$lat > 20.11954 & x$long < -118.0002 & x$long > -118.0072,]

#2


0  

You can subset across data frames by placing them into other other subsettable objects. Arrays and lists are used to hold them.

您可以通过将数据框放入其他其他可子集化对象来对数据框进行分组。数组和列表用于保存它们。

lst <- list(df1, df2, df3)

Then you can subset with functions. For example, the first column of each data frame:

然后你可以用函数进行子集化。例如,每个数据框的第一列:

lapply(lst, `[`, 1)
[[1]]
        V1
1 20.08824
2 20.09489
3 20.11476
4 20.12138

[[2]]
         V1
1 -118.0981
2 -117.9931
3 -117.8092
4 -117.7830

[[3]]
  V1
1  2
2  2
3  4
4  6

The same first column subset can be done with an array:

可以使用数组完成相同的第一列子集:

a <- simplify2array(c(df1, df2, df3))
dim(a) <- c(4,4,3)
a[,1,]