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@ispa.pt
barbarahcosta@gmail.com

	[[alternative HTML version deleted]]


