Dear R forum, I want to replace all the elements in a data frame (dd) which match the character "x" with "0". What's the most elegant way of doing this (there must be an easy way which I've missed)? I settled on the following loop:>for(i in 5:12){ # These are the column of dd I am interestedin>dd[which(dd[,i]=="x"),i]<-0 >}The problem with this is that the columns which used to contain "x" are still considered factors and I am unable to coerce them into numeric:> mean.species.biomass<-colMeans(as.numeric(dd.p[,5:12])) >Error in inherits(x, "data.frame") :(list) object cannot be coerced to type 'double' I'm tried unclassing & reclassing, other functions etc. but nothing seems to work. What is wrong? Thanks in advance, Allen -- View this message in context: http://n4.nabble.com/Class-attributes-tp948693p948693.html Sent from the R help mailing list archive at Nabble.com.
Here a way of doing it: for (i in 5:12){ # convert to character so you can substitute 'x' a <- as.character(dd[,i]) a[a == 'x'] <- '0' replace with zero dd[,i] <- as.numeric(a) } On Fri, Dec 4, 2009 at 11:55 AM, Allen L <allen.larocque@gmail.com> wrote:> > Dear R forum, > I want to replace all the elements in a data frame (dd) which match the > character "x" with "0". > What's the most elegant way of doing this (there must be an easy way which > I've missed)? I settled on the following loop: > > >for(i in 5:12){ # These are the column of dd I am > interested > in > >dd[which(dd[,i]=="x"),i]<-0 > >} > > The problem with this is that the columns which used to contain "x" are > still considered factors and I am unable to coerce them into numeric: > > > mean.species.biomass<-colMeans(as.numeric(dd.p[,5:12])) > >Error in inherits(x, "data.frame") : > (list) object cannot be coerced to type 'double' > > I'm tried unclassing & reclassing, other functions etc. but nothing seems > to > work. What is wrong? > Thanks in advance, > Allen > -- > View this message in context: > http://n4.nabble.com/Class-attributes-tp948693p948693.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]
On Dec 4, 2009, at 11:55 AM, Allen L wrote:> > Dear R forum, > I want to replace all the elements in a data frame (dd) which match > the > character "x" with "0". > What's the most elegant way of doing this (there must be an easy way > which > I've missed)? I settled on the following loop: > >> for(i in 5:12){ # These are the column of dd I am >> interested > in >> dd[which(dd[,i]=="x"),i]<-0 >> } > > The problem with this is that the columns which used to contain "x" > are > still considered factors and I am unable to coerce them into numeric:Converting the labels of fac.obj (a factor object) to numeric values is a FAQ (I forget the number, 7.<something>) as.numeric(as.character( fac.obj )) Factors are internally sets of numbers, but those numbers are not generally what people assign meaning to, and certainly not in your case where you assigned the character "0" to those rows where the labels were previously "x".> >> mean.species.biomass<-colMeans(as.numeric(dd.p[,5:12])) >> Error in inherits(x, "data.frame") : > (list) object cannot be coerced to type 'double' > > I'm tried unclassing & reclassing, other functions etc. but nothing > seems to > work. What is wrong? > Thanks in advance, > Allen > ---- David Winsemius, MD Heritage Laboratories West Hartford, CT