Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

dataframe

r

cell

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

like image 403
Per Månsson Avatar asked Feb 08 '13 22:02

Per Månsson


People also ask

How do you change a DataFrame value in Python?

Pandas DataFrame update() Method The update() method updates a DataFrame with elements from another similar object (like another DataFrame). Note: this method does NOT return a new DataFrame. The updating is done to the original DataFrame.

How do I change data value in R?

In the R Commander, you can click the Data set button to select a data set, and then click the Edit data set button. For more advanced data manipulation in R Commander, explore the Data menu, particularly the Data / Active data set and Data / Manage variables in active data set menus.


2 Answers

data.frame[row_number, column_number] = new_value 

For example, if x is your data.frame:

x[1, 4] = 5 
like image 113
Marcel Hebing Avatar answered Sep 17 '22 13:09

Marcel Hebing


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 
like image 20
Mohamed Rahouma Avatar answered Sep 17 '22 13:09

Mohamed Rahouma