62

How can one 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 I'm using the data.frame on several occasions.

starball
  • 20,030
  • 7
  • 43
  • 238
Per Månsson
  • 701
  • 2
  • 7
  • 8

4 Answers4

57
data.frame[row_number, column_number] = new_value

For example, if x is your data.frame:

x[1, 4] = 5
Marcel Hebing
  • 3,072
  • 1
  • 19
  • 22
37

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
Mohamed Rahouma
  • 1,084
  • 9
  • 20
9

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

iris$Sepal.Length[3]=999
userJT
  • 11,486
  • 20
  • 77
  • 88
7

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!