Omar Gonzales
Omar Gonzales

Reputation: 4008

R: Using "Gsub" a lost my data frame format

I have this Data Frame:

  Campaña           Visitas  Compras
1 faceBOOKAds-1     524       2
2 FacebookAds-2      487      24
3 fcebookAds-3      258       4
4 Email1            8         7 

And i want this:

  Campaña    Visitas  Compras
1 FBAds1     524       2
2 FBAds2     487      24
3 FBAds3     258       4
4 Email1       8       7

1) I've read that "GSUB" would do the work so i've used this:

DataGoogle2 <- gsub("faceBOOKAds-1", "FBAds", DataGoogle1$Campaña)

But i get this vector object (As you see i've lost my data.frame format):

[1] "FBAds"              "FacebookAds-2"      "fcebookAds-3"      "Email1" ...

2) Then i try to use: as.data.frame:

DataGoogle2 <- as.data.frame(gsub("faceBOOKAds-1", "FBAds", DataGoogle1$Campaña))

But get this (No data frame format):

1                                                FBAds
2                                        fFacebookAds-2
3                                        fcebookAds-3
4                                               Email1 

How can i get what i need? I know that the replacement method is not so good. What i need the most is not to loose the Data Frame format, but any help with the REGEX part is welcome!

Upvotes: 0

Views: 2072

Answers (3)

Rich Scriven
Rich Scriven

Reputation: 99351

You could also do direct replacement on the first column. This replaces the desired parts in the first column only by operating on the first column only. And this will keep the desired data frame structure.

> dat[[1]] <- gsub("f(.*)[-]", "FBAds", dat[[1]], ignore.case = TRUE)
> dat
#   Campaña Visitas Compras
# 1  FBAds1     524       2
# 2  FBAds2     487      24
# 3  FBAds3     258       4
# 4  Email1       8       7

...presuming your original data is called dat.

Upvotes: 0

user3694373
user3694373

Reputation: 140

When you used the following code:

DataGoogle2 <- gsub("faceBOOKAds-1", "FBAds", DataGoogle1$Campaña)

R reads that you want to your data frame to consist of DataGoogle1$Campaña column only and hence you get that output.

In stead, try this:

DataGoogle2$Campaña <- gsub("faceBOOKAds-1", "FBAds", DataGoogle1$Campaña)

This way you are saying that you want the COLUMN and not the DATA FRAME to be transformed. In any code, your LHS of expression is as important as your RHS code.

Hope this helps.

Upvotes: 0

Sven Hohenstein
Sven Hohenstein

Reputation: 81713

You can use transform (and another regex).

DataGoogle2 <- transform(DataGoogle1, Campaña = sub("(?i)fa?cebook(.*)-(.*)", 
                                                    "FB\\1\\2", Campaña))
#   Campaña Visitas Compras
# 1  FBAds1     524       2
# 2  FBAds2     487      24
# 3  FBAds3     258       4
# 4  Email1       8       7

The functions sub and gsub return a vector. Hence, the information of all other columns is not present in the output. With transform you can modify columns of an existing data frame and return a new one.

In the regex, (?i) starts the non-case sensitive mode. Furthermore, I used sub since I assume that there is never more than one match per string.

Upvotes: 1

Related Questions