[R] ifelse() question
Adaikalavan Ramasamy
ramasamy at cancer.org.uk
Fri Oct 29 19:44:06 CEST 2004
Francisco, a more reproducible example would have helped but see if the
following helps in your understanding
# Create dataset
df <- data.frame( type=1:5, species=LETTERS[1:5] )
df
type species
1 1 A
2 2 B
3 3 C
4 4 D
5 5 E
df[ ,2] == "E"
[1] FALSE FALSE FALSE FALSE TRUE
# Note that you need to coerce as.character inside ifelse
attach(df)
(df[ ,2] <- ifelse( species == "E", type, as.character(species) ))
[1] "A" "B" "C" "D" "5"
detach(df)
df
type species
1 1 A
2 2 B
3 3 C
4 4 D
5 5 5
On Fri, 2004-10-29 at 18:03, F Z wrote:
> 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.
> >______________________________________________________
>
> ______________________________________________
> 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
>
More information about the R-help
mailing list