How do I change a single value in a data.frame?

RDataframeCell

R Problem Overview


Could anyone explain how to change a single cell in a data.frame to something else. Basically I just want to rename that one cell, not all cells which matches it. I can´t use the edit() command because it will screw up my script since im using the data.frame on several occasions.

Thanks in advance

R Solutions


Solution 1 - R

data.frame[row_number, column_number] = new_value

For example, if x is your data.frame:

x[1, 4] = 5

Solution 2 - R

Suppose your dataframe is df and you want to change gender from 2 to 1 in participant id 5 then you should determine the row by writing "==" as you can see

 df["rowName", "columnName"] <- value
 df[df$serial.id==5, "gender"] <- 1

Solution 3 - R

To change a cell value using a column name, one can use

iris$Sepal.Length[3]=999

Solution 4 - R

In RStudio you can write directly in a cell. Suppose your data.frame is called myDataFrame and the row and column are called columnName and rowName. Then the code would look like:

myDataFrame["rowName", "columnName"] <- value

Hope that helps!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPer M&#229;nssonView Question on Stackoverflow
Solution 1 - RMarcel HebingView Answer on Stackoverflow
Solution 2 - RMohamed RahoumaView Answer on Stackoverflow
Solution 3 - RuserJTView Answer on Stackoverflow
Solution 4 - RAnne van VughtView Answer on Stackoverflow