【发布时间】:2021-03-02 11:43:36
【问题描述】:
我有一个数据框,其中包含超过 90.0000 个位置(纬度和经度)以及潜水深度,我想要两件事。
#I wrote this example above to who gonna help can try at your R program.
df = data.table(dive = c(10, 15, 20, 50, 70, 80, 90, 40, 100, 40, 40,
50, 67, 45, 70, 30),
lat = c(-23, -24, -25, -26, -27, -28, -29, -30, -32,
-33, -34, -35, -36, -37, -38, -39),
lon = c(-44, -43, -42, -41, -40, -39, -38, -35, -30,
-28, -25, -23, -20, -19, -18, -15))
#the class of all of this is numeric
-第一
为了让你更好地理解,我有这张地图,圆圈是给定位置的潜水深度:
我有潜水深度,我想选择一个特定区域(纬度 = -36,纬度 = - 27,经度 = -27 和经度 = -40)。
我想在数据框中以蓝色选择该区域内的潜水深度:
- 第二
现在,我需要选择绿色的反向区域:
我尝试了什么
我尝试这样做以选择“蓝色”区域:
df2<-df[df$lat <= -36 & df$lat >= -27 & df$lon >= -27 & df$lon <= -40]
#this return the data frame with no variables and with all locations
#And I tried inserting a comma at the end
df2<-df[df$lat <= -36 & df$lat >= -27 & df$lon >= -27 & df$lon <= -40,]
#this return the data frame with no observations
有人知道怎么做吗? 谢谢!
【问题讨论】:
-
你有 negative 数字,当你将
>更改为<和>到<它应该工作:df[df$lat >= -36 & df$lat <= -27 & df$lon <= -27 & df$lon >= -40,]和df[!(df$lat >= -36 & df$lat <= -27 & df$lon <= -27 & df$lon >= -40),]
标签: r dataframe select subset multiple-conditions