Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

dataframe

r

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 
like image 282
sam Avatar asked Oct 31 '13 17:10

sam


People also ask

How do I add a new variable to an existing data frame in R?

Add new Variables to a Data Frame using Existing Variables in R Programming – mutate() Function. mutate() function in R Language is used to add new variables in a data frame which are formed by performing operation on existing variables.

How do I add a single value to a DataFrame in R?

Adding Single Observation / Row To R Data FrameCreate a new Data Frame of the same number of variables/columns. Name the newly created Data Frame variable as of old Data Frame in which you want to add this observation. Use the rbind() function to add a new observation.


1 Answers

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).

like image 100
Jilber Urbina Avatar answered Nov 04 '22 20:11

Jilber Urbina