Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing text in a data frame

Tags:

r

I am working with a data frame in which I need to edit the entries in one particular column to allow for easy sorting. The data frame looks like this when imported:

     Assay    Genotype Description Sample   Operator
1    CCT6-18  C    A.Conservative  1_062911 Automatic   
2    CCT6-24  C       E.User Call  1_062911   charles
3    CCT6-25  A    A.Conservative  1_062911 Automatic

I need to change the assay column from CCT6-18 to CCT6-018. This "assay" appears multiple times within the data frame and I'd like to change all of the entries at once. Ive tried the gsub function but it returns data in a format that I am unfamiliar with. I'd like to get the data back in a data frame.

Help!

like image 207
Sam Globus Avatar asked Oct 03 '11 23:10

Sam Globus


People also ask

How do I change text in a data frame?

You can replace a string in the pandas DataFrame column by using replace(), str. replace() with lambda functions.

How do you replace something in a data frame?

Pandas DataFrame replace() MethodThe replace() method replaces the specified value with another specified value. The replace() method searches the entire DataFrame and replaces every case of the specified value.

How do I change text in a DataFrame in R?

To change a text value in an R data frame, we can simply use replace function.

Can we modify data inside a data frame?

Using Python replace() method, we can update or change the value of any string within a data frame. We need not provide the index or label values to it. As seen above, we have replaced the word “Siri” with “Code” within the dataframe.


1 Answers

df$Assay <- replace(df$Assay, df$Assay=="CCT6-18", "CCT6-018")

Should see you right.

Also, try str(df) or class(df$Assay) to see what class your Assay column is. If it is a factor this could be the reason you're getting tripped up. If so run df$Assay <- as.character(df$Assay) first.

like image 74
nzcoops Avatar answered Sep 30 '22 06:09

nzcoops