Hello, I have a data frame with 400 columns and wanted to filter character columns with "$" in it.For example:?>? x <- c("$5", "$89", "$10", "$34")??>? y <- c(1:4)>? My.Data <- data.frame (x,y)> My.Data? ? x y1? $5 12 $89 23 $10 34 $34 4 I want to detect the columns with $ and remove the $ from the selected columns.I have tried?apply(My.Data, 2, function (x) any(grepl("$", x))) but it's not really working.Or:??apply(My.Data, 2, function(x){x<-gsub("\\$", "", x)}) works but it turns all the columns to a factor. Thanks. [[alternative HTML version deleted]]
Hi Farnoosh, Perhaps this will help: drop_dollar<-function(x) return(as.numeric(as.character(gsub("\\$","",x)))) sapply(My.Data,drop_dollar) Jim On Thu, Apr 19, 2018 at 7:23 AM, Farnoosh Sheikhi via R-help <r-help at r-project.org> wrote:> Hello, > I have a data frame with 400 columns and wanted to filter character columns with "$" in it.For example: > x <- c("$5", "$89", "$10", "$34") > y <- c(1:4)> My.Data <- data.frame (x,y)> My.Data x y1 $5 12 $89 23 $10 34 $34 4 > I want to detect the columns with $ and remove the $ from the selected columns.I have tried apply(My.Data, 2, function (x) any(grepl("$", x))) but it's not really working.Or: apply(My.Data, 2, function(x){x<-gsub("\\$", "", x)}) works but it turns all the columns to a factor. > Thanks. > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Your message came through all messed up because you did not tell your email program to use plain text format. This at best delays a responds and at worst prevents us from understanding your question as you intended. 1) The columns became factors when you created the data frame because you did not include the stringsAsFactors=FALSE argument. The apply function does NOT turn the columns to factors. 2) Your use of "x <-" in your apply function is misleading, though not destructive. Just omit that assignment. dta2 <- apply(My.Data, 2, function (x) gsub("\\$", "", x))) On April 18, 2018 4:23:09 PM CDT, Farnoosh Sheikhi via R-help <r-help at r-project.org> wrote:>Hello, >I have a data frame with 400 columns and wanted to filter character >columns with "$" in it.For example:?>? x <- c("$5", "$89", "$10", >"$34")??>? y <- c(1:4)>? My.Data <- data.frame (x,y)> My.Data? ? x y1? >$5 12 $89 23 $10 34 $34 4 >I want to detect the columns with $ and remove the $ from the selected >columns.I have tried?apply(My.Data, 2, function (x) any(grepl("$", x))) >but it's not really working.Or:??apply(My.Data, 2, >function(x){x<-gsub("\\$", "", x)}) works but it turns all the columns >to a factor. >Thanks. > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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.-- Sent from my phone. Please excuse my brevity.
Hello, To the OP: The dollar sign is a meta character, so it must be escaped, that is what was wrong with your code. The right regular expression would be grepl("\\$", x) When a regular expression doesn't work, try reading the help page ?regex. Another good source you can try is https://regex101.com Hope this helps, Rui Barradas On 4/18/2018 11:19 PM, Jim Lemon wrote:> Hi Farnoosh, > Perhaps this will help: > > drop_dollar<-function(x) return(as.numeric(as.character(gsub("\\$","",x)))) > sapply(My.Data,drop_dollar) > > Jim > > On Thu, Apr 19, 2018 at 7:23 AM, Farnoosh Sheikhi via R-help > <r-help at r-project.org> wrote: >> Hello, >> I have a data frame with 400 columns and wanted to filter character columns with "$" in it.For example: > x <- c("$5", "$89", "$10", "$34") > y <- c(1:4)> My.Data <- data.frame (x,y)> My.Data x y1 $5 12 $89 23 $10 34 $34 4 >> I want to detect the columns with $ and remove the $ from the selected columns.I have tried apply(My.Data, 2, function (x) any(grepl("$", x))) but it's not really working.Or: apply(My.Data, 2, function(x){x<-gsub("\\$", "", x)}) works but it turns all the columns to a factor. >> Thanks. >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >