Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace characters from a column of a data frame R

I have a data frame

a <- runif (10) b <- letters [1:10] c <- c(rep ("A-B", 4), rep("A_C", 6)) data1 <- data.frame (a, b, c) data1 

And I wan to replace _ in A_C of column c for - to have a data frame like data2:

z <- c(rep ("A-B", 4), rep("A-C", 6)) data2 <- data.frame (a, b, z) data2 

Do you know how I can do that?

like image 710
AEM Avatar asked Jan 17 '14 13:01

AEM


People also ask

How do I replace values in a column in a Dataframe in R?

Method 1: Using Replace() function. replace() function in R Language is used to replace the values in the specified string vector x with indices given in list by those given in values.

How do I remove a character from a column in R?

To remove a character in an R data frame column, we can use gsub function which will replace the character with blank. For example, if we have a data frame called df that contains a character column say x which has a character ID in each value then it can be removed by using the command gsub("ID","",as.

How do I replace a text in a Dataframe in R?

To replace the character column of dataframe in R, we use str_replace() function of “stringr” package.


2 Answers

Use gsub:

data1$c <- gsub('_', '-', data1$c) data1              a b   c 1  0.34597094 a A-B 2  0.92791908 b A-B 3  0.30168772 c A-B 4  0.46692738 d A-B 5  0.86853784 e A-C 6  0.11447618 f A-C 7  0.36508645 g A-C 8  0.09658292 h A-C 9  0.71661842 i A-C 10 0.20064575 j A-C 
like image 110
tonytonov Avatar answered Sep 19 '22 10:09

tonytonov


If your variable data1$c is a factor, it's more efficient to change the labels of the factor levels than to create a new vector of characters:

levels(data1$c) <- sub("_", "-", levels(data1$c))               a b   c 1  0.73945260 a A-B 2  0.75998815 b A-B 3  0.19576725 c A-B 4  0.85932140 d A-B 5  0.80717115 e A-C 6  0.09101492 f A-C 7  0.10183586 g A-C 8  0.97742424 h A-C 9  0.21364521 i A-C 10 0.02389782 j A-C 
like image 43
Sven Hohenstein Avatar answered Sep 18 '22 10:09

Sven Hohenstein