Hi! I will calculate sum??s in the following way: E.g.: a <- rpois(100,20) b <- rpois(100,5) x <- data.frame(cbind(a,b)) # the sum??s should be calculated based on a. attach(x) sort.nace <- unique(sort(x[,1])) sum1 <- matrix(ncol=1, nrow=length(sort.nace)) # I calculate the sum of all values of b, which have the same category in a. Eg.: sum1[1,] <- sum(subset(x, a==sort.nace[1], select=b), na.rm=TRUE) # or: sum1[5,] <- sum(subset(x, a==sort.nace[5], select=b), na.rm=TRUE) # all is ok. # but when I do it in a loop, I get an error message. sum1 <- matrix(ncol=1, nrow=length(sort.nace)) for(i in 1:length(nace)){ sum1[i,] <- sum(subset(x, a==sort.nace[i], select=b), na.rm=TRUE) } #Error in Summary.data.frame(..., na.rm = na.rm) : # only defined on a data frame with all numeric or complex variables # but sum1 was correct calculated(!): # sum1 # [,1] # [1,] 3 # [2,] 4 # [3,] 8 # [4,] 7 # [5,] 41 # [6,] 57 # [7,] 38 # [8,] 15 # [9,] 44 # [10,] 72 # [11,] 57 # [12,] 27 # [13,] 12 # [14,] 24 # [15,] 29 # [16,] 16 # [17,] 19 # [18,] 3 # [19,] 4 # [20,] 8 # [21,] 6 The variables of x are of class numeric. My function, which should calculate these sum??s, does not work, because of this error. Can anybody please tell me, what I??m doing wrong? (I think it would be better to write a sapply instead of the loop, but I have two functions (sum and subset) and it is very hard for me to use the sapply here correctly) Thanks, Matthias
Dimitris Rizopoulos
2004-Jun-29 08:38 UTC
[R] Calculate correctly, but gives an error message
Hi Matthias, I think you just need, lapply(split(b, a), sum, na.rm=TRUE) I hope this helps. Best, Dimitris ---- Dimitris Rizopoulos Doctoral Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/396887 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "TEMPL Matthias" <Matthias.Templ at statistik.gv.at> To: <R-help at stat.math.ethz.ch> Sent: Tuesday, June 29, 2004 10:27 AM Subject: [R] Calculate correctly, but gives an error message> Hi! > > I will calculate sum??s in the following way: > E.g.: > > > a <- rpois(100,20) > b <- rpois(100,5) > > x <- data.frame(cbind(a,b)) > > # the sum??s should be calculated based on a. > > attach(x) > > sort.nace <- unique(sort(x[,1])) > > sum1 <- matrix(ncol=1, nrow=length(sort.nace)) > > # I calculate the sum of all values of b, which have the samecategory in a. Eg.:> > sum1[1,] <- sum(subset(x, a==sort.nace[1], select=b), na.rm=TRUE) > > # or: > > sum1[5,] <- sum(subset(x, a==sort.nace[5], select=b), na.rm=TRUE) > > # all is ok. > # but when I do it in a loop, I get an error message. > > sum1 <- matrix(ncol=1, nrow=length(sort.nace)) > for(i in 1:length(nace)){ > sum1[i,] <- sum(subset(x, a==sort.nace[i], select=b), na.rm=TRUE) > } > #Error in Summary.data.frame(..., na.rm = na.rm) : > # only defined on a data frame with all numeric or complexvariables> > # but sum1 was correct calculated(!): > > # sum1 > # [,1] > # [1,] 3 > # [2,] 4 > # [3,] 8 > # [4,] 7 > # [5,] 41 > # [6,] 57 > # [7,] 38 > # [8,] 15 > # [9,] 44 > # [10,] 72 > # [11,] 57 > # [12,] 27 > # [13,] 12 > # [14,] 24 > # [15,] 29 > # [16,] 16 > # [17,] 19 > # [18,] 3 > # [19,] 4 > # [20,] 8 > # [21,] 6 > > The variables of x are of class numeric. > My function, which should calculate these sum??s, does not work,because of this error.> > Can anybody please tell me, what I??m doing wrong? > > (I think it would be better to write a sapply instead of the loop,but I have two functions (sum and subset) and it is very hard for me to use the sapply here correctly)> > Thanks, > Matthias > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide!http://www.R-project.org/posting-guide.html
Prof Brian Ripley
2004-Jun-29 08:41 UTC
[R] Calculate correctly, but gives an error message
You are using an undefined object `nace'. If I use sort.nace, it works> sum1 <- matrix(ncol=1, nrow=length(sort.nace)) > for(i in 1:length(sort.nace)){ # not length(nace)+ sum1[i,] <- sum(subset(x, a==sort.nace[i], select=b), na.rm=TRUE) + }>I think you were trying to access rows beyond your matrix, so the error occurred after sum1 was filled. I have little idea what you are trying to do: might it be tapply(x$b, a, sum)? At least that gives the same answer as the corrected code. On Tue, 29 Jun 2004, TEMPL Matthias wrote:> Hi! > > I will calculate sum??s in the following way: > E.g.: > > > a <- rpois(100,20) > b <- rpois(100,5) > > x <- data.frame(cbind(a,b)) > > # the sum??s should be calculated based on a. > > attach(x) > > sort.nace <- unique(sort(x[,1])) > > sum1 <- matrix(ncol=1, nrow=length(sort.nace)) > > # I calculate the sum of all values of b, which have the same category in a. Eg.: > > sum1[1,] <- sum(subset(x, a==sort.nace[1], select=b), na.rm=TRUE) > > # or: > > sum1[5,] <- sum(subset(x, a==sort.nace[5], select=b), na.rm=TRUE) > > # all is ok. > # but when I do it in a loop, I get an error message. > > sum1 <- matrix(ncol=1, nrow=length(sort.nace)) > for(i in 1:length(nace)){ > sum1[i,] <- sum(subset(x, a==sort.nace[i], select=b), na.rm=TRUE) > } > #Error in Summary.data.frame(..., na.rm = na.rm) : > # only defined on a data frame with all numeric or complex variables > > # but sum1 was correct calculated(!): > > # sum1 > # [,1] > # [1,] 3 > # [2,] 4 > # [3,] 8 > # [4,] 7 > # [5,] 41 > # [6,] 57 > # [7,] 38 > # [8,] 15 > # [9,] 44 > # [10,] 72 > # [11,] 57 > # [12,] 27 > # [13,] 12 > # [14,] 24 > # [15,] 29 > # [16,] 16 > # [17,] 19 > # [18,] 3 > # [19,] 4 > # [20,] 8 > # [21,] 6 > > The variables of x are of class numeric. > My function, which should calculate these sum??s, does not work, because of this error. > > Can anybody please tell me, what I??m doing wrong? > > (I think it would be better to write a sapply instead of the loop, but I have two functions (sum and subset) and it is very hard for me to use the sapply here correctly) > > Thanks, > Matthias > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595