[R] GSUB function and regex problem

William Dunlap wdunlap at tibco.com
Mon Aug 18 17:14:09 CEST 2014


gsub will work on a column of a data.frame, not an entire data.frame.
> gsub(pattern = "facebook-Ads1", "FBAds", DataGoogle1$Campaña)
[1] "FBAds"         "faceBOOK-Ads1" "fcebook-ads12" "Email1"
"mail1"
[6] "referral1"

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Mon, Aug 18, 2014 at 2:13 AM, Omar André Gonzáles Díaz
<oma.gonzales at gmail.com> wrote:
> Hi all, i have this data.frame with 3 columns: "campaña", "visitas",
> "compras".
>
> This is the actual data.frame:
>
>  Campaña                  Visitas       Compras
> 1 facebook-Ads1       524                   2
> 2 faceBOOK-Ads1     487                   24
> 3 fcebook-ads12        258                  4
> 4        Email1              8                      7
> 5         mail1               224                  2
> 6     referral1              147                  7
>
> And i want to replace all the entries on the "campaña" :
>
> from this: "facebook-Ads1" or this: "faceBOOK-Ads1"--> to this: "FBAds".
>
> These are my steps:
>
> 1) I load the CSV file into R:
>
> DataGoogle1 <- read.csv(file = "DataGoogle2.csv", header = T)
>
> 2) and then use:
>
> gsub("facebook-Ads1", "FBAds", DataGoogle1)
>
> But the result is not what i'm expecting:
>
> i get:
>
>> gsub(pattern = "facebook-Ads1", "FBAds", DataGoogle1)
> [1] "c(2, 3, 4, 1, 5, 6, 7, 8, 9)"                "c(524, 487, 258, 8, 224,
> 147, 87, 863, 145)"
> [3] "c(2, 24, 4, 7, 2, 7, 5, 23, 14)"
>
> May you help me to understand why it is not working? Thanks! :D
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list