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
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With