[R] ifelse() question
F Z
gerifalte28 at hotmail.com
Fri Oct 29 19:03:55 CEST 2004
Thanks for you reply Peter. I tried using as.character and then converting
to factors but it did not work since it generates missing values for all the
dat[,4]=="POR". See:
>dat[1:5,4]
[1] BOV POR BOV POR BOV
Levels: BOV CAP CER OVI POR
test<-dat
>test[,4]<-as.character(test[,4])
>test[,5]<-as.character(test[,5])
>test[test[,4]=="POR",4]<-test[test[,4]=="POR",5]
Error in "[<-.data.frame"(`*tmp*`, test[, 4] == "POR", 4, value = c(NA, :
missing values are not allowed in subscripted assignments of data
frames
>test[,4]<-as.factor(test[,4])
>test[,5]<-as.factor(test[,5])
>test[1:5,4]
[1] BOV <NA> BOV <NA> BOV
Levels: BOV CAP CER OVI
Any suggestions?
Thanks again!
Francisco
>From: "Peter Alspach" <PAlspach at hortresearch.co.nz>
>To: <gerifalte28 at hotmail.com>,<R-help at stat.math.ethz.ch>
>Subject: Re: [R] ifelse() question
>Date: Fri, 29 Oct 2004 13:33:54 +1300
>
>
>Francisco
>
>Did you try changing the factors to character, with as.character?
>
>Also you don't really need ifelse() for this. Something like the
>following (untested) should do it:
>
>dat[,4] <- as.character(dat[,4])
>dat[,5] <- as.character(dat[,5])
>dat[dat[,4]=='POR',4] <- dat[dat[,4]=='POR',5]
>dat[,4] <- as.factor(dat[,4])
>dat[,5] <- as.factor(dat[,5])
>
>
>Peter Alspach
>
> >>> "F Z" <gerifalte28 at hotmail.com> 29/10/04 12:48:54 >>>
>Hi
>
>I have a data.frame with dim = 18638 (rows) 6 (cols)
>
>names(dat)
>[1] "id" "long" "lat" "species" "type" "size"
>
>Variable "species" and "type" are factors. Species has 5 levels "BOV"
>"CAP"
>"CER" "OVI" "POR"
>Variable "type" has 11 levels "BRD" "CL" ... "OTHER"
>
>I would like to replace the values on species by the values on types
>only if
>species is == "POR"
>I tried:
>
>x<-ifelse(dat$species %in% "POR",dat$type,dat$species)
>dat[,4]<-x
>but levels(x)
>[1] "1" "2" "3" "4" "5" "6" "8" "9" "10" "11" "12"
>
>So x changes the factor names by numbers. I can not use factor() to
>recover
>the names since the resulting factors in x are a mixture of factors
>from
>species and type.
>
>I also tried
>
>x<-gsub(pattern = "POR",replacement= factor(dat$type),dat$species)
>with
>same behavior.
>
>Apparently I did not have my granola bar today so I can't find a
>solution!
>Any help is greatly appreciated
>
>Thanks!
>
>Francisco
>
>______________________________________________
>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
>
>______________________________________________________
>
>The contents of this e-mail are privileged and/or confidential to the
>named recipient and are not to be used by any other person and/or
>organisation. If you have received this e-mail in error, please notify
>the sender and delete all material pertaining to this e-mail.
>______________________________________________________
More information about the R-help
mailing list