2016-03-03 36 views
0

我从csv文件中调用height,diameter和age。我试图用pi x h x r^2来计算树的体积。为了计算半径,我将dbh除以2,然后我得到这个错误。如何在R中使用tapply()使用aritmatic()

错误DBH/2:非数字参数二元运算

setwd("/Users/user/Desktop/") 
treeg <- read.csv("treeg.csv",row.names=1) 
head(treeg) 

heights <- tapply(treeg$height.ft,treeg$forest, identity) 
ages <- tapply(treeg$age,treeg$forest, identity) 
dbh  <- tapply(treeg$dbh.in,treeg$forest, identity) 

radius <- dbh/2 

在矢量DBH它存储所述直径从他林这是ID方面CSV文件。

如何将dbh除以2,同时仍然保留每个值的格式(通过其接受的ID存储)(这是他的林---> treeg $ forest),而treeg是调用csv文件的数据帧。

> head(treeg) 
    tree.ID forest habitat dbh.in height.ft age 
1  1  4  5 14.6  71.4 55 
2  1  4  5 12.4  61.4 45 
3  1  4  5 8.8  40.1 35 
4  1  4  5 7.0  28.6 25 
5  1  4  5 4.0  19.6 15 
6  2  4  5 20.0  103.4 107 

str(dbh) 
List of 9 
$ 1: num [1:36] 19.9 18.6 16.2 14.2 12.3 9.4 6.8 4.9 2.6 22 ... 
$ 2: num [1:60] 16.5 15.5 14.5 13.7 12.7 11.4 9.5 8 5.9 4.1 ... 
$ 3: num [1:50] 18.4 17.2 15.6 13.7 11.6 8.5 5.3 2.8 13.3 10.6 ... 
$ 4: num [1:81] 14.6 12.4 8.8 7 4 20 18.8 17 15.9 14 ... 
$ 5: num [1:153] 28 27.2 26.1 25 23.7 21.3 19 16.7 12.2 9.8 ... 
$ 6: num [1:22] 21.3 20.2 19.1 18 16.9 15.6 14.8 13.3 11.3 9.2 ... 
$ 7: num [1:63] 13.9 12.4 10.6 8.1 5.8 3.4 27 25.6 23 20.2 ... 
$ 8: num [1:27] 20.8 17.7 15.6 13.2 10.5 7.5 4.8 2.9 12.9 11.3 ... 
$ 9: num [1:50] 23.6 20.5 16.9 14.1 11.1 8 5.1 2.9 24.1 20.9 ... 
- attr(*, "dim")= int 9 
- attr(*, "dimnames")=List of 1 
    ..$ : chr [1:9] "1" "2" "3" "4" ... 
+4

你能张贴'头(treeg)'和'可能STR(胸径)'的输出?更一般地说,我建议读这个:http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – coffeinjunky

+0

Shady ...我们不会做你的功课的人。 – cory

+1

与'tapply'一起使用'identity'非常不正常。你为什么这样做? – nicola

回答

2

你只是想创建一个半径栏,dbh.in除以二?

treeg <- read.table(textConnection("tree.ID forest habitat dbh.in height.ft age 
1  1  4  5 14.6  71.4 55 
2  1  4  5 12.4  61.4 45 
3  1  4  5 8.8  40.1 35 
4  1  4  5 7.0  28.6 25 
5  1  4  5 4.0  19.6 15 
6  2  4  5 20.0  103.4 107"), header=TRUE) 

treeg$radius <- treeg$dbh.in/2 

或你需要的是胸径列表的东西...

dbh  <- tapply(treeg$dbh.in,treeg$forest, identity) 
> dbh 
$`4` 
[1] 14.6 12.4 8.8 7.0 4.0 20.0 

lapply(dbh, function(x)x/2) 
List of 1 
$ 4: num [1:6] 7.3 6.2 4.4 3.5 2 10