Dividing values ​​in a column of a data frame into values ​​from another data frame when the row values ​​correspond

I have data.frame xwith the following format:

     species      site  count
1:         A       1.1     25
2:         A       1.2   1152
3:         A       2.1     26
4:         A       3.5      1
5:         A       3.7     98
---                         
101:       B       1.2      6
102:       B       1.3     10
103:       B       2.1      8
104:       B       2.2      8
105:       B       2.3      5

I also have another data.frame areawith the following format:

      species    area
1:          A    59.7
2:          B    34.4
3:          C    37.7
4:          D    22.8

I would like to split the column count data.frame xby the values ​​in the column area data.frame areawhen the values ​​in the view column of each data.framecorrespond

I am trying to get it to work with a function ddply:

density = ddply(x, "species", mutate, density = x$count/area[,2]

But I can’t understand the correct index call syntax area[]to select only the row that matches the values ​​found in x$species. However, I'm a super beginner in the package plyr(and apply*in general), so this may be a completely wrong approach

I hope to return the data.framefollowing format:

     species      site  count   density
1:         A       1.1     25     0.419
2:         A       1.2    152     2.546
3:         A       2.1     26     0.436
4:         A       3.5      1     0.017
5:         A       3.7     98     1.641
---                         
101:       B       1.2      6     0.174
102:       B       1.3     10     0.291
103:       B       2.1      8     0.233
104:       B       2.2      8     0.233
105:       B       2.3      5     0.145
+4
2

data.table:

library(data.table)
#converting your data to the native type for the package (by reference)
setDT(x); setDT(area) 
x[area, density:=count/i.area, on="species"]

:= - data.table ( , . , b) ), x:=y x data.table y.

X[Y,] y x ; , y data.table, x y j (.. ), density:=count/area; , y, i., , i, . .

, , " ", . data.table . .

+6

(left_join), , mutate:

library(dplyr)

x %>% left_join(area, by="species") %>%
      mutate(density = count/area)
+5

Source: https://habr.com/ru/post/1608577/


All Articles