0

I'm calling height, diameter and age from a csv file. I'm trying to calculate the volume of the tree using pi x h x r^2. In order to calculate the radius, I'm taking dbh and dividing it by 2. Then I get this error.

Error in dbh/2 : non-numeric argument to binary operator

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

In the vector dbh it is storing the diameter from he csv file in terms of forest which is the ID.

How can I divide dbh by 2, while still retaining format of each value being stored by its receptive ID (which is he forest ---> treeg$forest) and treeg is the dataframe that call the csv file.

> 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
    Could you post the output of `head(treeg)` and possibly `str(dbh)`? And more generally, I'd recommend reading this: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – coffeinjunky Mar 03 '16 at 14:11
  • Shady... we aren't gonna do your homework man. – cory Mar 03 '16 at 14:23
  • 1
    Using `identity` with `tapply` is pretty anomalous. Why are you doing it? – nicola Mar 03 '16 at 14:32
  • Who said this was homework. I'm asking to try help me to understand how to fix an error. I'm not asking you to write a whole essay. Thanks coffeinjunky for the help. – Simeon Jaganyi Mar 03 '16 at 14:53
  • Ninja edit alert.... it's homework based on the file path you edited to change. `setwd("/Users/user/Desktop/Assignment 3")` – cory Mar 03 '16 at 16:18
  • Homework or not, it looks like a question about a particular programming error to me. I certainly agree that it is a poorly structured one, but why would we care what the motivation behind the programming attempt itself is? – coffeinjunky Mar 03 '16 at 16:23
  • cory, Hey man, while programming I ran into an error, an error that has taken me hours to fix with no prevail. I decided to post my question here to see if anyone could help me understand were I'm going wrong. I didn't expect you to criticize me for asking for help. I'm not asking you to do my homework/project/assignment whatever you want to call it, I'm just asking for help. Thanks coffeinjunky for not judging. – Simeon Jaganyi Mar 03 '16 at 17:30
  • You are gonna have to get a much thicker skin to survive this place... – cory Mar 03 '16 at 18:03
  • Don't feed the trolls. – Adam Hoelscher Mar 03 '16 at 18:08
  • From `str(dbh)`, you see that this object is a `list` (look at where it says `List of 9`). You can't divide a list by 2, hence the error. Instead you need to access the values stored in that list. See corey's answer below for one approach on how to do it. – coffeinjunky Mar 04 '16 at 09:59

1 Answers1

2

Are you just trying to create a radius column that is dbh.in divided by two?

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

Or do you need that dbh list for something...

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
cory
  • 6,529
  • 3
  • 21
  • 41