Drescher, Michael (MNR)
2007-Jul-05 23:59 UTC
[R] problem assigning to indexed data frame element
Hi All, Sorry if I ask an obvious thing, I am still new to R ... I created a data frame of given dimensions to which I gave strings as column names. I want to write to elements of the data frame by indexing them with the row number and column name (string). The problem is that I can read elements from the data frame in this way, but I cannot assign to elements in this way. Instead, I get the following error message: Error in Summary.factor(..., na.rm = na.rm) : min not meaningful for factors Please find the code I used farther below. It would be great if someone could help me. Best regards, Michael PS: Coincidentally, I found this same error message mentioned in another context (levelplot) as indicating a bug (original bug report PR# 6005 on Mon, 22 Dec 2003) ------------------------ Michael Drescher Ontario Forest Research Institute Ontario Ministry of Natural Resources 1235 Queen St East Sault Ste Marie, ON, P6A 2E3 Tel: (705) 946-7406 Fax: (705) 946-2030 ------------------------ Code:> sfalls.plot.comp <- matrix(nrow=plot.count, ncol=spec.count, byrow=T) > colnames(sfalls.plot.comp) <- levels(SPECIES)### SPECIES, SPP_VOL, & PLOT are columns/variables in a previously read data file> sfalls.plot.comp <- data.frame(sfalls.plot.comp) > attach(sfalls.plot.comp) > sfalls.plot.comp[is.na(sfalls.plot.comp)] <- 0> sfalls.plot.compBf Bw Pj Po Sb 1 0 0 0 0 0 2 0 0 0 0 0> hh <- 1 > current.spec <- SPECIES[hh]; current.vol <- SPP_VOL[hh]; current.plot<- PLOT[hh]> current.spec[1] Bf Levels: Bf Bw Pj Po Sb> current.vol[1] 2> current.plot[1] 1> sfalls.plot.comp[current.plot,current.spec]### thus, reading from the data frame in this way (using the column name/string) works fine [1] 0> sfalls.plot.comp[current.plot,current.spec] <- current.vol ### butassigning in this way does not work Error in Summary.factor(..., na.rm = na.rm) : min not meaningful for factors> sfalls.plot.comp[current.plot,1] <- current.vol### assigning by using the column number instead of the column name of course does work> sfalls.plot.comp[current.plot,current.spec][1] 2> sfalls.plot.comp[current.plot,"Bw"] <- current.vol### as does assigning when replacing 'current.spec' for its assigned value in quotes, e.g., "Bw"> sfalls.plot.comp[current.plot,"Bw"][1] 2> sfalls.plot.compBf Bw Pj Po Sb 1 2 2 0 0 0 2 0 0 0 0 0
Check what is happening with current.spec. It looks to me as if you are trying to use a factor as an index. See below --- "Drescher, Michael (MNR)" <michael.drescher at ontario.ca> wrote:> Hi All, > > Sorry if I ask an obvious thing, I am still new to R > ... > > I created a data frame of given dimensions to which > I gave strings as > column names. I want to write to elements of the > data frame by indexing > them with the row number and column name (string). > The problem is that I > can read elements from the data frame in this way, > but I cannot assign > to elements in this way. Instead, I get the > following error message: > > Error in Summary.factor(..., na.rm = na.rm) : > min not meaningful for factors > > Please find the code I used farther below. It would > be great if someone > could help me. > > Best regards, Michael > > PS: Coincidentally, I found this same error message > mentioned in another > context (levelplot) as indicating a bug (original > bug report PR# 6005 on > Mon, 22 Dec 2003) > > ------------------------ > Michael Drescher > Ontario Forest Research Institute > Ontario Ministry of Natural Resources > 1235 Queen St East > Sault Ste Marie, ON, P6A 2E3 > Tel: (705) 946-7406 > Fax: (705) 946-2030 > > ------------------------ > > Code: > > sfalls.plot.comp <- matrix(nrow=plot.count, > ncol=spec.count, byrow=T) > > colnames(sfalls.plot.comp) <- levels(SPECIES) > ### SPECIES, SPP_VOL, & PLOT are columns/variables > in a previously read > data file > > sfalls.plot.comp <- data.frame(sfalls.plot.comp) > > attach(sfalls.plot.comp) > > sfalls.plot.comp[is.na(sfalls.plot.comp)] <- 0 > > > sfalls.plot.comp > Bf Bw Pj Po Sb > 1 0 0 0 0 0 > 2 0 0 0 0 0 > > > hh <- 1 > > current.spec <- SPECIES[hh]; current.vol <- > SPP_VOL[hh]; current.plot > <- PLOT[hh] > > > current.spec > [1] Bf > Levels: Bf Bw Pj Po Sb > > > current.vol > [1] 2 > > > current.plot > [1] 1 > > > sfalls.plot.comp[current.plot,current.spec] > ### thus, reading from the data frame in this way > (using the column > name/string) works fine > [1] 0 > > > sfalls.plot.comp[current.plot,current.spec] <- > current.vol ### but > assigning in this way does not work > Error in Summary.factor(..., na.rm = na.rm) : > min not meaningful for factorsIf I am reading this correctly current.spec is not a column name or a number. It is a factor as the levels indicate. You might be able to get around this by current.spec <- as.character(SPECIES[hh]) R has,what to me is, an annoying tendency to read in many columns of characters as factors. This can be changed by changing something in the startup file. Exactly what I have forgotten.> > > sfalls.plot.comp[current.plot,1] <- current.vol > ### assigning by using the column number instead of > the column name of > course does work > > sfalls.plot.comp[current.plot,current.spec] > [1] 2 > > > sfalls.plot.comp[current.plot,"Bw"] <- current.vol > ### as does assigning when replacing 'current.spec' > for its assigned > value in quotes, e.g., "Bw" > > sfalls.plot.comp[current.plot,"Bw"] > [1] 2 > > > sfalls.plot.comp > Bf Bw Pj Po Sb > 1 2 2 0 0 0 > 2 0 0 0 0 0 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, > reproducible code. >