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

4

63

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.

Higgle answered 8/2, 2013 at 22:13 Comment(0)
C
58
data.frame[row_number, column_number] = new_value

For example, if x is your data.frame:

x[1, 4] = 5
Contrived answered 8/2, 2013 at 22:21 Comment(2)
Is it possibe to edit cell value by column name and row name?Pruritus
@Pruritus data_frame["row_name", "col_name"]=2Urethrectomy
C
40

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
Casimiracasimire answered 12/8, 2019 at 15:20 Comment(2)
Most useful answer of the lot.Prehension
@LuísdeSousa Thanks a lotCasimiracasimire
S
9

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

iris$Sepal.Length[3]=999
Silvern answered 1/12, 2016 at 20:57 Comment(0)
A
8

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!

Areopagite answered 11/1, 2018 at 11:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.