Change text in data frame

I am working with a data frame in which I need to edit records in a single column to make sorting easier. The data frame is as follows:

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 analysis column from CCT6-18 to CCT6-018. This “analysis” appears several times in the data frame, and I would like to immediately change all the records. Ive tried the gsub function, but it returns data in a format that I am not familiar with. I would like to return data to a data frame.

Help!

+4
source share
3 answers
 df$Assay <- replace(df$Assay, df$Assay=="CCT6-18", "CCT6-018") 

Must see you right.

Also, try str(df) or class(df$Assay) to find out which class the analysis column is in. If this is a factor, it may be the reason that you work. If you first run df$Assay <- as.character(df$Assay) .

+5
source

It depends on whether you want to change other entries in Assay . The easy way is to simply add 0 after the dash:

 df$Assay <- gsub('-', '-0', df$Assay) 

A regular solution would be something like:

 df$Assay <- gsub('(\\d\\d)','0\\1', df$Assay) 

This will replace any two digits with 0 , followed by the same two digits. You should be careful with regular expressions because you need to know your data well to be sure that you will not change anything wrong. For example, if you have CCT62-18 as an entry in Assay , you will not want to use this regular expression because it will change the number from 62 to 062.

+3
source

I would go by replacing the factor level.

 sam <- data.frame(assay = c("CCT6-18", "CCT6-23", "CCT6-25"), genetype = sample(letters, 3), operator = runif(3), sample = runif(3)) str(sam) 'data.frame': 3 obs. of 4 variables: $ assay : Factor w/ 3 levels "CCT6-18","CCT6-23",..: 1 2 3 $ genetype: Factor w/ 3 levels "f","u","w": 1 2 3 $ operator: num 0.595 0.912 0.76 $ sample : num 0.525 0.626 0.377 levels(sam$assay)[1] <- "CCT6-018" sam assay genetype operator sample 1 CCT6-018 f 0.5950434 0.5249502 2 CCT6-23 u 0.9123185 0.6257186 3 CCT6-25 w 0.7595744 0.3769029 
+1
source

All Articles