You should be looking at the ifelse function. Ihe if and else
constructs are not appropriate for recoding because they do not return
vectors.
Much easier than all that folderol would be:
df$Aggregation <- ifelse( df$Number>1, "School",
"Solitary")
On May 4, 2009, at 12:31 PM, barbara horta e costa wrote:
> Hi everyone,
>
> Do you know how to apply a If command to a vector in a data.frame,
> creating
> a new column?
>
> Example: If one species has "1" value in the Abundance column
then
> it is
> "Solitary". If the abundance is higher than 1, is
"School".
>
> I did:
>
> aggregationFunction <-function(x){
> (if (x>1) a<- 'School'
> else a<- 'Solitary')
> return (a)
> }
>
> Then:
>
> Aggregation <- lapply (df$Number, aggregationFunction )
>
>
>
> Then:
>
> df <- data.frame (df, Aggregation)
>
>
>
> And I got this error:
> Error in data.frame(df, Aggregation) :
> arguments imply differing number of rows: 2576, 1
>
>
>
> Can you help me doing this function to a vector? I?ve tried to find
> this
> question and the answer online but I couldn't.
>
>
>
> I tried also only with if but it is only for 1 length and the error
> was:
>
> the condition has length > 1 and only the first element will be used
>
>
>
> This question seemed easy in the beginning but I was not successful.
>
>
>
> Thanks a lot for helping me.
>
> Barbara
>
>
> --
> B?rbara H. Costa
> Marine Biologist Researcher
> SCIAENA - Marine Sciences and Cooperation
> www.sciaena.org
>
> bcosta at ispa.pt
> barbarahcosta at gmail.com
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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
> and provide commented, minimal, self-contained, reproducible code.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT