3

I am totally new in R and have the following problem: I want to create a new column V4 with the letter A only IF values in V2 are bigger or equal 1.2

This is my test data frame df:

    V1  V2  V3
1   ABC 1.2 4.3
2   CFS 2.3 1.7
3   dgf 1.3 4.4

That's what I did

df$V4<-NA

for(i in 1:nrow(df)) {   
 xy=df[i,]$V2   
 if (grepl(>=1.2,xy)) 
 df[i,]$V4 ="A"      
}  
}

It works fine when I just want exactly 1.2, but >= seems not to work. Has anyone an idea why?

Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
RNewbi
  • 83
  • 1
  • 6
  • Welcome to SO and the `R` community on here! Since you are new here you may find it useful to read [**the about**](http://stackoverflow.com/about) and the [**faq**](http://stackoverflow.com/faq) about how SO works to get the most out of the site. – Simon O'Hanlon May 01 '13 at 09:42

1 Answers1

3

An ifelse seems appropriate in this instance:

df$V4 <- ifelse( df$V2 >= 1.2 , "A" , NA )

This will give NA to values that are not >= to 1.2. In your example all values in df$V2 are greater than or equal to 1.2 so you get:

 df
#   V1  V2  V3 V4
#1 ABC 1.2 4.3  A
#2 CFS 2.3 1.7  A
#3 dgf 1.3 4.4  A

However if we make a value that is smaller than 1.2...

df
#   V1  V2  V3
#1 ABC 1.2 4.3
#2 CFS 2.3 1.7
#3 dgf 1.0 4.4

df$V4 <- ifelse( df$V2 >= 1.2 , "A" , NA )
df
#   V1  V2  V3   V4
#1 ABC 1.2 4.3    A
#2 CFS 2.3 1.7    A
#3 dgf 1.0 4.4 <NA>

The NA is surround by angled braces to denote that it is infact an NA (missing value etc) rather than the character string 'NA'.

If you have a large number of rows in your dataset then a comparison and replacement of a subset method could be quicker:

 df$V4 <- NA
 df$V4[ df$V2 >= 1.2 ] <- "A"
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
  • Is [**this answer**](http://stackoverflow.com/questions/16253789/what-is-the-difference-between-na-and-na/16253827#16253827) entirely right then? Here the column `V4` is of type character and still we get angle brackets around NA. – Arun May 01 '13 at 09:45