简体   繁体   English

如何在r的数据帧内的组中应用/循环函数(在本例中为chull())?

[英]How to apply/loop function, in this case chull(), to groups within a dataframe in r?

I have a dataframe like this: 我有一个这样的数据框:

x2 <- c(12,-10,-3,-5,-3, 18,-14,-3,-13,14,12,-10,-3,-5,-3, 18,-14,-3,-13,14)
y2 <- c(-3,-4,-11,-12,-13,-4,5,-10,-3,6,-3,-4,-11,-12,-13,-4,5,-10,-3,6)
ID2 <- c(5088,5088,5088,5088,5088,5088,5088,5088,5088,5088,6000,6000,6000,6000,6000,6000,6000,6000,6000,6000)
D2 <- c(59,49,70,40,74,78,90,55,65,73,59,49,70,40,74,78,90,55,65,73)
Code2 <- c(110,110,110,130,110,110,110,110,110,100,110,110,110,130,110,110,110,110,110,100)
df2 <- data.frame(x2,y2,ID2,D2,Code2)

df2
    x2  y2  ID2 D2 Code2
1   12  -3 5088 59   110
2  -10  -4 5088 49   110
3   -3 -11 5088 70   110
4   -5 -12 5088 40   130
5   -3 -13 5088 74   110
6   18  -4 5088 78   110
7  -14   5 5088 90   110
8   -3 -10 5088 55   110
9  -13  -3 5088 65   110
10  14   6 5088 73   100
11  12  -3 6000 59   110
12 -10  -4 6000 49   110
13  -3 -11 6000 70   110
14  -5 -12 6000 40   130
15  -3 -13 6000 74   110
16  18  -4 6000 78   110
17 -14   5 6000 90   110
18  -3 -10 6000 55   110
19 -13  -3 6000 65   110
20  14   6 6000 73   100
...

x and y are cartesian coordinates of trees within an ensemble of trees. xy是树木集合中树木的笛卡尔坐标。 ID is each of these ensembles' individual identification. ID是这些乐团的个人标识。 Code and D are parameters which are not relevant yet. CodeD是尚不相关的参数。

Now I am trying to apply the fuction chull() to each ensemble in order to get a data.frame that consists of only those trees that form the boundary of the ensembles. 现在,我尝试将功能chull()应用于每个合奏,以获取一个data.frame ,该数据仅由构成合奏边界的那些树组成。 Something like this for all ID's: 所有ID都类似这样:

x1 <- c(12,-10,-3,-5,-3, 18,-14,-3,-13,14)
y1 <- c(-3,-4,-11,-12,-13,-4,5,-10,-3,6)
ID1 <- c(5088,5088,5088,5088,5088,5088,5088,5088,5088,5088)
D1 <- c(59,49,70,40,74,78,90,55,65,73)
Code1 <- c(110,110,110,130,110,110,110,110,110,100)
df1 <- data.frame(x1,y1,ID1,D1,Code1)
hullpts <- chull(df1)
df1[hullpts,]

    x1  y1  ID1 D1 Code1
6   18  -4 5088 78   110
5   -3 -13 5088 74   110
4   -5 -12 5088 40   130
9  -13  -3 5088 65   110
7  -14   5 5088 90   110
10  14   6 5088 73   100

I've been trying to create the loop with for() and nlme::gapply() unfortunately without any success. 我一直在尝试用for()nlme::gapply()创建循环, nlme::gapply()没有成功。

I would be very grateful for any help. 我将非常感谢您的帮助。

I don't know how familiar you are with external packages, but with data.table it would be a simple function like: 我不知道您对外部软件包有多熟悉,但是对于data.table,它将是一个简单的函数,例如:

library(data.table)

#group by ID2 and then apply chull to x and y in each group
#.SD just references the groups created by grouping by ID2
#setDT converts df2 to data.table
setDT(df2)[, .SD[chull(x2, y2),], by = 'ID2']
 #    ID2  x2  y2 D2 Code2
 #1: 5088  18  -4 78   110
 #2: 5088  -3 -13 74   110
 #3: 5088  -5 -12 40   130
 #4: 5088 -13  -3 65   110
 #5: 5088 -14   5 90   110
 #6: 5088  14   6 73   100
 #7: 6000  18  -4 78   110
 #8: 6000  -3 -13 74   110
 #9: 6000  -5 -12 40   130
#10: 6000 -13  -3 65   110
#11: 6000 -14   5 90   110
#12: 6000  14   6 73   100

Or if you want to use base R you would probably need to do something like: 或者,如果您想使用基数R,则可能需要执行以下操作:

splits <- split(df2, df2$ID2)
chulls <- 
  lapply(splits, function(x) {
    x[chull(x$x2, x$y2)]
  })
do.call(rbind, chulls)
 #    x2  y2  ID2 D2 Code2
 #1:  18  -4 5088 78   110
 #2:  -3 -13 5088 74   110
 #3:  -5 -12 5088 40   130
 #4: -13  -3 5088 65   110
 #5: -14   5 5088 90   110
 #6:  14   6 5088 73   100
 #7:  18  -4 6000 78   110
 #8:  -3 -13 6000 74   110
 #9:  -5 -12 6000 40   130
#10: -13  -3 6000 65   110
#11: -14   5 6000 90   110
#12:  14   6 6000 73   100

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

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