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]]
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.
Hello, Try reading your data with option stringsAsFactors = FALSE. It seems that your strings are being read as factors, which are coded as integers. DataGoogle1 <- read.csv(file = "DataGoogle2.csv", header = T, stringsAsFactors = FALSE) Hope this helps, Rui Barradas Em 18-08-2014 10:13, Omar Andr? Gonz?les D?az escreveu:> 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. >