anyone know what is wrong with my code?

set.seed(1)
data<-data.frame(year =rep(2000:2002,each = 6), count = round(runif(18, 0, 20)))
data
year count
1 2000 5
2 2000 7
3 2000 11
4 2000 18
5 2000 4
6 2000 18
7 2001 19
8 2001 13
9 2001 13
10 2001 1
11 2001 4
12 2001 4
13 2002 14
14 2002 8
15 2002 15
16 2002 10
17 2002 14
18 2002 20
>ddply(data, "year", function(x){mean<-mean(x$count) sd<-sd(x$count) cv<- sd/mean data.frame(cv.count = cv)})

So I keep getting an error from the last line. But I can’t figure it out by myself what is wrong with this code
I’m using rstudio cloud.

you may set every <- syntax a new line, or you should use ; to separate them in one line:

 > plyr::ddply(data, "year", function(x) {
+   mean <- mean(x$count) 
+   sd <- sd(x$count) 
+   cv <- sd / mean
+   data.frame(cv.count = cv)
+ })
  year  cv.count
1 2000 0.6288066
2 2001 0.6711441
3 2002 0.6233821

or

> plyr::ddply(data, "year", function(x) {mean <- mean(x$count);sd <- sd(x$count) ;cv <- sd / mean;data.frame(cv.count = cv)})
  year  cv.count
1 2000 0.6288066
2 2001 0.6711441
3 2002 0.6233821
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.