[R] ifelse() question

F Z
Fri, 29 Oct 2004 22:08:24 -0000

Many thanks to Peter, Sundar and Adaikalavan for their useful help.  As
Adaikalavan correctly pointed out, the key was to pass as.character() as an
argument within the ifelse() function.  Here is the soution using a subset
of my data:

dat[1:10,]
      id      long      lat species type size
1  91029 -95.29819 46.41441     BOV  FRM   NA
2  13468 -95.09137 46.26823     POR   FP    0
3  92511 -95.29784 44.47016     BOV  FRM   NA
4  30312 -94.97496 44.41489     POR  FTF  120
5  90275 -93.73471 44.92226     BOV  FRM   NA
6  38367 -95.54004 44.02396     POR   FF   NA
7  90460 -94.34028 44.89793     BOV  FRM   NA
8  38564 -93.51084 43.65327     OVI   CL   75
9  33532 -93.06094 45.76132     POR  FTF   NA
10 11860 -95.23439 45.21244     POR   FP    0

test<-dat[1:10,]
attach(test)
test[,4]<-ifelse(species=="POR",as.character(type),as.character(species))
test
      id      long      lat species type size
1  91029 -95.29819 46.41441     BOV  FRM   NA
2  13468 -95.09137 46.26823      FP   FP    0
3  92511 -95.29784 44.47016     BOV  FRM   NA
4  30312 -94.97496 44.41489     FTF  FTF  120
5  90275 -93.73471 44.92226     BOV  FRM   NA
6  38367 -95.54004 44.02396      FF   FF   NA
7  90460 -94.34028 44.89793     BOV  FRM   NA
8  38564 -93.51084 43.65327     OVI   CL   75
9  33532 -93.06094 45.76132     FTF  FTF   NA
10 11860 -95.23439 45.21244      FP   FP    0

It worked! Now all the species =="POR" were replaced by the adjacent
value
on the column type.

Thanks again!

Francisco


>From: Adaikalavan Ramasamy <ramasamy at cancer.org.uk>
>Reply-To: ramasamy at cancer.org.uk
>To: F Z <gerifalte28 at hotmail.com>
>CC: R-help <r-help at stat.math.ethz.ch>
>Subject: Re: [R] ifelse() question
>Date: Fri, 29 Oct 2004 18:44:06 +0100
>
>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
> >
>
>______________________________________________
>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