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

落花浮王杯 提交于 2019-11-27 11:09:23

问题


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


回答1:


data.frame[row_number, column_number] = new_value

For example, if x is your data.frame:

x[1, 4] = 5



回答2:


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

iris$Sepal.Length[3]=999



回答3:


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!




回答4:


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


来源:https://stackoverflow.com/questions/14782206/how-do-i-change-a-single-value-in-a-data-frame

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!