sam
sam

Reputation: 1436

Updating individual values (not rows) in an R data.frame

I would like to update values of var3 in an R data.frame mydata according to a simple criterion.

   var1  var2  var3
1  1     4     5
2  3     58    800
3  8     232   8 

I would think that the following should do:

mydata$var3[mydata$var3 > 500,] <- NA

However, this replaces the entire row of every matching record with NA (all cells of the row), instead of just the var3 value (cell):

   var1  var2  var3
1  1     4     5
2  NA    NA    NA
3  8     232   8 

How can I ensure that just the value for the selected variable is replaced? mydata should then look like

   var1  var2  var3
1  1     4     5
2  3     58    NA
3  8     232   8 

Upvotes: 6

Views: 39974

Answers (1)

Jilber Urbina
Jilber Urbina

Reputation: 61154

Use which and arr.ind=TRUE

> mydata[which(mydata[,3]>500, arr.ind=TRUE), 3] <- NA
> mydata
  var1 var2 var3
1    1    4    5
2    3   58   NA
3    8  232    8

Or just modify your previous attempt...

mydata[mydata$var3 > 500, 3] <- NA 

This also works

mydata$var3[mydata$var3 > 500 ] <- NA   # note no comma is inside [ ]

Your attempt didnt work because mydata$var3 gives a vector and you are indexing it as if it were a matrix by using [mydata$var3 > 500,] so a dimension error is thrown. You almost got it, all you have to do is remove the comma in your code (see my last alternative).

Upvotes: 18

Related Questions