[R] problem assigning to indexed data frame element
Drescher, Michael (MNR)
michael.drescher at ontario.ca
Fri Jul 6 01:59:26 CEST 2007
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 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.comp
Bf Bw Pj Po Sb
1 2 2 0 0 0
2 0 0 0 0 0
More information about the R-help
mailing list