Re: [R] find and
Here is a version similar to Rui's, but using ave() and logical indexing to simplify a bit: > DF4[with(DF4, ave(as.numeric(var), city, FUN = function(x)length(unique(x))) > ==1), ] city wk var 4 city2 1 x 5 city2 2 x 6 city2 3 x 7 city2 4 x 8 city3 1 x 9 city3 2 x 10 city3 3 x 11 city3 4 x 16 city5 3 - 17 city5 4 - Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sat, Mar 18, 2017 at 7:15 AM, Rui Barradas wrote: > Hello, > > I believe this does it. > > > sp <- split(DF4, DF4$city) > want <- do.call(rbind, lapply(sp, function(x) > if(length(unique(x$var)) == 1) x else NULL)) > rownames(want) <- NULL > want > > > Hope this helps, > > Rui Barradas > > > Em 18-03-2017 13:51, Ashta escreveu: >> >> Hi all, >> >> I am trying to find a city that do not have the same "var" value. >> Within city the var should be the same otherwise exclude the city from >> the final data set. >> Here is my sample data and my attempt. City1 and city4 should be excluded. >> >> DF4 <- read.table(header=TRUE, text=' city wk var >> city1 1 x >> city1 2 - >> city1 3 x >> city2 1 x >> city2 2 x >> city2 3 x >> city2 4 x >> city3 1 x >> city3 2 x >> city3 3 x >> city3 4 x >> city4 1 x >> city4 2 x >> city4 3 y >> city4 4 y >> city5 3 - >> city5 4 -') >> >> my attempt >> test2 <- data.table(DF4, key="city,var") >> ID1<- test2[ !duplicated(test2),] >> dps <- ID1$city[duplicated(ID1$city)] >> Ddup <- which(test2$city %in% dps) >> >> if(length(Ddup) !=0) { >>test2 <- test2[- Ddup,] } >> >> want <- data.frame(test2) >> >> >> I want get the following result but I am not getting it. >> >> city wk var >>city2 1 x >>city2 2 x >>city2 3 x >>city2 4 x >>city3 1 x >>city3 2 x >> city3 3 x >> city3 4 x >> city5 3 - >> city5 4 - >> >> Can some help me out the problem is? >> >> Thank you. >> >> __ >> R-help@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@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@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.
Re: [R] find and
Thank you Rudi and Ulrik. Rudi, your option worked for the small data set but when I applied to the big data set it taking long and never finished and have to kill it. I dont know why. Ulrik's option worked fine for the big data set (> 1.5M records) and took less than 2 minutes. These two are giving me the same results. # Counting unique DF4 %>%group_by(city) %>% filter(length(unique(var)) == 1) # Counting not duplicated DF4 %>%group_by(city) %>%filter(sum(!duplicated(var)) == 1) Thank yo again. On Sat, Mar 18, 2017 at 10:40 AM, Ulrik Stervbo wrote: > Using dplyr: > > library(dplyr) > > # Counting unique > DF4 %>% > group_by(city) %>% > filter(length(unique(var)) == 1) > > # Counting not duplicated > DF4 %>% > group_by(city) %>% > filter(sum(!duplicated(var)) == 1) > > HTH > Ulrik > > > On Sat, 18 Mar 2017 at 15:17 Rui Barradas wrote: >> >> Hello, >> >> I believe this does it. >> >> >> sp <- split(DF4, DF4$city) >> want <- do.call(rbind, lapply(sp, function(x) >> if(length(unique(x$var)) == 1) x else NULL)) >> rownames(want) <- NULL >> want >> >> >> Hope this helps, >> >> Rui Barradas >> >> Em 18-03-2017 13:51, Ashta escreveu: >> > Hi all, >> > >> > I am trying to find a city that do not have the same "var" value. >> > Within city the var should be the same otherwise exclude the city from >> > the final data set. >> > Here is my sample data and my attempt. City1 and city4 should be >> > excluded. >> > >> > DF4 <- read.table(header=TRUE, text=' city wk var >> > city1 1 x >> > city1 2 - >> > city1 3 x >> > city2 1 x >> > city2 2 x >> > city2 3 x >> > city2 4 x >> > city3 1 x >> > city3 2 x >> > city3 3 x >> > city3 4 x >> > city4 1 x >> > city4 2 x >> > city4 3 y >> > city4 4 y >> > city5 3 - >> > city5 4 -') >> > >> > my attempt >> > test2 <- data.table(DF4, key="city,var") >> > ID1<- test2[ !duplicated(test2),] >> > dps <- ID1$city[duplicated(ID1$city)] >> > Ddup <- which(test2$city %in% dps) >> > >> > if(length(Ddup) !=0) { >> >test2 <- test2[- Ddup,] } >> > >> > want <- data.frame(test2) >> > >> > >> > I want get the following result but I am not getting it. >> > >> > city wk var >> >city2 1 x >> >city2 2 x >> >city2 3 x >> >city2 4 x >> >city3 1 x >> >city3 2 x >> > city3 3 x >> > city3 4 x >> > city5 3 - >> > city5 4 - >> > >> > Can some help me out the problem is? >> > >> > Thank you. >> > >> > __ >> > R-help@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@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@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.
Re: [R] find and
Using dplyr: library(dplyr) # Counting unique DF4 %>% group_by(city) %>% filter(length(unique(var)) == 1) # Counting not duplicated DF4 %>% group_by(city) %>% filter(sum(!duplicated(var)) == 1) HTH Ulrik On Sat, 18 Mar 2017 at 15:17 Rui Barradas wrote: > Hello, > > I believe this does it. > > > sp <- split(DF4, DF4$city) > want <- do.call(rbind, lapply(sp, function(x) > if(length(unique(x$var)) == 1) x else NULL)) > rownames(want) <- NULL > want > > > Hope this helps, > > Rui Barradas > > Em 18-03-2017 13:51, Ashta escreveu: > > Hi all, > > > > I am trying to find a city that do not have the same "var" value. > > Within city the var should be the same otherwise exclude the city from > > the final data set. > > Here is my sample data and my attempt. City1 and city4 should be > excluded. > > > > DF4 <- read.table(header=TRUE, text=' city wk var > > city1 1 x > > city1 2 - > > city1 3 x > > city2 1 x > > city2 2 x > > city2 3 x > > city2 4 x > > city3 1 x > > city3 2 x > > city3 3 x > > city3 4 x > > city4 1 x > > city4 2 x > > city4 3 y > > city4 4 y > > city5 3 - > > city5 4 -') > > > > my attempt > > test2 <- data.table(DF4, key="city,var") > > ID1<- test2[ !duplicated(test2),] > > dps <- ID1$city[duplicated(ID1$city)] > > Ddup <- which(test2$city %in% dps) > > > > if(length(Ddup) !=0) { > >test2 <- test2[- Ddup,] } > > > > want <- data.frame(test2) > > > > > > I want get the following result but I am not getting it. > > > > city wk var > >city2 1 x > >city2 2 x > >city2 3 x > >city2 4 x > >city3 1 x > >city3 2 x > > city3 3 x > > city3 4 x > > city5 3 - > > city5 4 - > > > > Can some help me out the problem is? > > > > Thank you. > > > > __ > > R-help@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@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. > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] find and
Hello, I believe this does it. sp <- split(DF4, DF4$city) want <- do.call(rbind, lapply(sp, function(x) if(length(unique(x$var)) == 1) x else NULL)) rownames(want) <- NULL want Hope this helps, Rui Barradas Em 18-03-2017 13:51, Ashta escreveu: Hi all, I am trying to find a city that do not have the same "var" value. Within city the var should be the same otherwise exclude the city from the final data set. Here is my sample data and my attempt. City1 and city4 should be excluded. DF4 <- read.table(header=TRUE, text=' city wk var city1 1 x city1 2 - city1 3 x city2 1 x city2 2 x city2 3 x city2 4 x city3 1 x city3 2 x city3 3 x city3 4 x city4 1 x city4 2 x city4 3 y city4 4 y city5 3 - city5 4 -') my attempt test2 <- data.table(DF4, key="city,var") ID1<- test2[ !duplicated(test2),] dps <- ID1$city[duplicated(ID1$city)] Ddup <- which(test2$city %in% dps) if(length(Ddup) !=0) { test2 <- test2[- Ddup,] } want <- data.frame(test2) I want get the following result but I am not getting it. city wk var city2 1 x city2 2 x city2 3 x city2 4 x city3 1 x city3 2 x city3 3 x city3 4 x city5 3 - city5 4 - Can some help me out the problem is? Thank you. __ R-help@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@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] find and
Hi all, I am trying to find a city that do not have the same "var" value. Within city the var should be the same otherwise exclude the city from the final data set. Here is my sample data and my attempt. City1 and city4 should be excluded. DF4 <- read.table(header=TRUE, text=' city wk var city1 1 x city1 2 - city1 3 x city2 1 x city2 2 x city2 3 x city2 4 x city3 1 x city3 2 x city3 3 x city3 4 x city4 1 x city4 2 x city4 3 y city4 4 y city5 3 - city5 4 -') my attempt test2 <- data.table(DF4, key="city,var") ID1<- test2[ !duplicated(test2),] dps <- ID1$city[duplicated(ID1$city)] Ddup <- which(test2$city %in% dps) if(length(Ddup) !=0) { test2 <- test2[- Ddup,] } want <- data.frame(test2) I want get the following result but I am not getting it. city wk var city2 1 x city2 2 x city2 3 x city2 4 x city3 1 x city3 2 x city3 3 x city3 4 x city5 3 - city5 4 - Can some help me out the problem is? Thank you. __ R-help@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.
Re: [R] Find and replace backslashes XXXX
On 27/05/2015 8:55 AM, Dan Abner wrote: > Hi Ista, > > Is there no way to not escape the backslash in the pathway? You don't need to escape it if you read it from a file, get it from list.files(), etc. You only need to escape it if you are writing a literal string in R code. Duncan Murdoch The > pathway is going to change and will become very long and I need to do > this programmatically. Beside, escaping the backslash defeats the > purpose of using gsub. If I could do this manually each and every > time, I would change simply change the backslash to a forward slash > and therefore not need gsub at all... > > Thanks, > > Dan > > On Tue, May 26, 2015 at 9:56 PM, Ista Zahn wrote: >> Escape the backslash with another backslash, i.e., >> >> gsub("\\","/","X:\\Classes\\TT\\Automation", fixed = TRUE) >> >> best, >> Ista >> >> On Tue, May 26, 2015 at 9:30 PM, Dan Abner wrote: >>> Hi all, >>> >>> I realize that the backslash is an escape character in R, therefore, I >>> am trying to replace it with a forward slash. Can someone please >>> suggest how to get this code to work? >>> lib<-gsub("\","/","X:\Classes\TT\Automation") >>> Error: unexpected symbol in "lib<-gsub("\","/","X" >>> >>> >>> Thanks, >>> >>> Dan >>> >>> __ >>> R-help@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@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@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.
Re: [R] Find and replace backslashes XXXX
Hi Ista, Is there no way to not escape the backslash in the pathway? The pathway is going to change and will become very long and I need to do this programmatically. Beside, escaping the backslash defeats the purpose of using gsub. If I could do this manually each and every time, I would change simply change the backslash to a forward slash and therefore not need gsub at all... Thanks, Dan On Tue, May 26, 2015 at 9:56 PM, Ista Zahn wrote: > Escape the backslash with another backslash, i.e., > > gsub("\\","/","X:\\Classes\\TT\\Automation", fixed = TRUE) > > best, > Ista > > On Tue, May 26, 2015 at 9:30 PM, Dan Abner wrote: >> Hi all, >> >> I realize that the backslash is an escape character in R, therefore, I >> am trying to replace it with a forward slash. Can someone please >> suggest how to get this code to work? >> >>> lib<-gsub("\","/","X:\Classes\TT\Automation") >> Error: unexpected symbol in "lib<-gsub("\","/","X" >> >> >> Thanks, >> >> Dan >> >> __ >> R-help@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@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.
Re: [R] Find and replace backslashes XXXX
Since the character looks like a Windows file path, you could use normalizePath() instead of gsub(). normalizePath("X:\\Classes\\TT\\Automation", winslash = "/", mustWork = FALSE) ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance Kliniekstraat 25 1070 Anderlecht Belgium To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey 2015-05-27 13:10 GMT+02:00 Duncan Murdoch : > On 26/05/2015 9:56 PM, Ista Zahn wrote: > > Escape the backslash with another backslash, i.e., > > > > gsub("\\","/","X:\\Classes\\TT\\Automation", fixed = TRUE) > > ... and note that if you want to use a regular expression (i.e. fixed = > FALSE), you would need another level of escaping, i.e. > > gsub("","/","X:\\Classes\\TT\\Automation") > > The second level of escaping is for the regexpr processor. It would > need to be done in the first or second strings, but not in the third, > which is the data. > > Duncan Murdoch > > > > > > best, > > Ista > > > > On Tue, May 26, 2015 at 9:30 PM, Dan Abner > wrote: > >> Hi all, > >> > >> I realize that the backslash is an escape character in R, therefore, I > >> am trying to replace it with a forward slash. Can someone please > >> suggest how to get this code to work? > >> > >>> lib<-gsub("\","/","X:\Classes\TT\Automation") > >> Error: unexpected symbol in "lib<-gsub("\","/","X" > >> > >> > >> Thanks, > >> > >> Dan > >> > >> __ > >> R-help@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@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@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. > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Find and replace backslashes XXXX
On 26/05/2015 9:56 PM, Ista Zahn wrote: > Escape the backslash with another backslash, i.e., > > gsub("\\","/","X:\\Classes\\TT\\Automation", fixed = TRUE) ... and note that if you want to use a regular expression (i.e. fixed = FALSE), you would need another level of escaping, i.e. gsub("","/","X:\\Classes\\TT\\Automation") The second level of escaping is for the regexpr processor. It would need to be done in the first or second strings, but not in the third, which is the data. Duncan Murdoch > > best, > Ista > > On Tue, May 26, 2015 at 9:30 PM, Dan Abner wrote: >> Hi all, >> >> I realize that the backslash is an escape character in R, therefore, I >> am trying to replace it with a forward slash. Can someone please >> suggest how to get this code to work? >> >>> lib<-gsub("\","/","X:\Classes\TT\Automation") >> Error: unexpected symbol in "lib<-gsub("\","/","X" >> >> >> Thanks, >> >> Dan >> >> __ >> R-help@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@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@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.
Re: [R] Find and replace backslashes XXXX
Escape the backslash with another backslash, i.e., gsub("\\","/","X:\\Classes\\TT\\Automation", fixed = TRUE) best, Ista On Tue, May 26, 2015 at 9:30 PM, Dan Abner wrote: > Hi all, > > I realize that the backslash is an escape character in R, therefore, I > am trying to replace it with a forward slash. Can someone please > suggest how to get this code to work? > >> lib<-gsub("\","/","X:\Classes\TT\Automation") > Error: unexpected symbol in "lib<-gsub("\","/","X" > > > Thanks, > > Dan > > __ > R-help@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@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] Find and replace backslashes XXXX
Hi all, I realize that the backslash is an escape character in R, therefore, I am trying to replace it with a forward slash. Can someone please suggest how to get this code to work? > lib<-gsub("\","/","X:\Classes\TT\Automation") Error: unexpected symbol in "lib<-gsub("\","/","X" Thanks, Dan __ R-help@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.
Re: [R] find and remove outlier
Dear Pushpa Methekar, You can use which.max(), as in > library(car) > mod <- lm(prestige~ income + education, data=Duncan) > which.max(abs(rstudent(mod))) minister 6 In fact, this is exactly what outlierTest() does, as you can see by looking at car:::outlierTest.lm Beyond that, I'd encourage you to look more carefully at potential outliers, including at the distribution of the studentized residuals, rather than adopting a simple mechanical rule for removing outliers. I hope this helps, John John Fox, Professor McMaster University Hamilton, Ontario, Canada http://socserv.mcmaster.ca/jfox/ On Mon, 5 Jan 2015 12:20:15 + "Methekar, Pushpa (GE Transportation, Non-GE)" wrote: > > > Hi, this is my function to find rstudent of model which will give me outlier > But I wonder if I could find out the exact no. of outlier in data set. > Like outlierTest() does from car package . > > > rm.outliers = function(dataset,model){ > dataset$rstudent = rstudent(model) > for(i in 1:length(dataset$rstudent)){ >if(dataset$rstudent[i]>=-3 & dataset$rstudent[i]<=3) > {print(dataset$rstudent[i])} > else { print("this is an outlier") > library(car) >print(outlierTest(model,n.max=1, order=TRUE) ) } > i<-i+1 > } > } > > > [[alternative HTML version deleted]] > > __ > R-help@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@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] find and remove outlier
Hi, this is my function to find rstudent of model which will give me outlier But I wonder if I could find out the exact no. of outlier in data set. Like outlierTest() does from car package . rm.outliers = function(dataset,model){ dataset$rstudent = rstudent(model) for(i in 1:length(dataset$rstudent)){ if(dataset$rstudent[i]>=-3 & dataset$rstudent[i]<=3) {print(dataset$rstudent[i])} else { print("this is an outlier") library(car) print(outlierTest(model,n.max=1, order=TRUE) ) } i<-i+1 } } [[alternative HTML version deleted]] __ R-help@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.
Re: [R] find and add text
Hi, No problem. Try: data.frame(col1=data,col2=as.character(factor(gsub("\\d+","",data),labels=c("positive","negative"))),stringsAsFactors=FALSE) A.K. Hello Arun, Thank you so much, it works great. But I some sets, the data contains some additional characters also, like a1, a2, a3b1,b2 and so on. in this case how to tag positive and negative values? initial data: data<- c("a1", "b1","b","b2","a2","a3","b","a","b3") output expected: col1 col2 a1 positive b1 negative b negative b2 negative a2 positive a positive b negative a positive b3 negative How to use grep and tag the annotations.. Thanks karthick On Saturday, October 26, 2013 3:10 PM, arun wrote: Hi, You may try: dat2 <- data.frame(col1=data,col2=as.character(factor(data,labels=c("positive","negative"))),stringsAsFactors=FALSE) A.K. Hello all, I have a data something like this; data<- c("a", "b","b","b","a","a","b","a","b") and I need to represent all "a"'s as "positive" and "b"'s "negative" in data.frame something like this; data.frame output= col1 col2 a positive b negative b negative b negative a positive a positive b negative a positive b negative Thanks in advance for your solutions Thanks karthick __ R-help@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. __ R-help@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.
Re: [R] find and add text
Hi, You may try: dat2 <- data.frame(col1=data,col2=as.character(factor(data,labels=c("positive","negative"))),stringsAsFactors=FALSE) A.K. Hello all, I have a data something like this; data<- c("a", "b","b","b","a","a","b","a","b") and I need to represent all "a"'s as "positive" and "b"'s "negative" in data.frame something like this; data.frame output= col1 col2 a positive b negative b negative b negative a positive a positive b negative a positive b negative Thanks in advance for your solutions Thanks karthick __ R-help@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.
Re: [R] find and replace missing data in several different files
Hello, I have a question and need your help urgently. I am new to R but want to learn it. I have several files in a folder which I have imported to R using : temp = list.files(pattern="*.txt") >myfiles = lapply(temp, read.delim) The resulting files are on the workspace stored as List[110]. So they are 110 files in the list. Each file has several different columns and rows. My question: I would like to find and replace -999.99M with NA; Find 'T','C','A','F' and 'Y', delete them from all the 110 files. Then, I want to write.table all the corrected files back to a folder on my computer. Thanks for your help. Atem. From: "r-help-requ...@r-project.org" Sent: Friday, April 26, 2013 11:08 AM Subject: confirm bfdc3137cee0135cf3c616295c9d0e2b3adfd392 Mailing list subscription confirmation notice for mailing list R-help We have received a request from 129.132.148.130 for subscription of r-help@r-project.org mailing list. To confirm that you want to be added to this mailing list, simply reply to this message, keeping the Subject: header intact. Or visit this web page: https://stat.ethz.ch/mailman/confirm/r-help/bfdc3137cee0135cf3c616295c9d0e2b3adfd392 Or include the following line -- and only the following line -- in a message to r-help-requ...@r-project.org: confirm bfdc3137cee0135cf3c616295c9d0e2b3adfd392 Note that simply sending a `reply' to this message should work from most mail readers, since that usually leaves the Subject: line in the right form (additional "Re:" text in the Subject: is okay). If you do not wish to be subscribed to this list, please simply disregard this message. If you think you are being maliciously subscribed to the list, or have any other questions, send them to r-help-ow...@r-project.org. [[alternative HTML version deleted]] __ R-help@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.
Re: [R] find and replace characters in a string
Although I am not an expert, this is simple. txt<- "LOI ." gsub(".","%",txt, fixed=TRUE) Regards Petr > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- > project.org] On Behalf Of Rui Barradas > Sent: Wednesday, March 27, 2013 5:17 PM > To: Shane Carey > Cc: r-help@r-project.org > Subject: Re: [R] find and replace characters in a string > > Hello, > > The period is a metacharacter so you have to escape it. > The period is escaped with a '\'. In it's turn, '\' is a metacharacter > so it needs to be escaped. Hence the double'\\'. > > x <- "LOI ." > gsub("\\.", "(%)", x) > > > Hope this helps, > > Rui Barradas > > Em 27-03-2013 16:09, Shane Carey escreveu: > > Hi, > > > > I have a string of text as follows "LOI ." > > > > How do I replace the dot with "(%)" > > > > gsub(".","(%)",LOI .) > > > > gives > > > > "(%)(%)(%)(%)(%)" > > > > Thanks > > > > __ > R-help@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. __ R-help@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.
Re: [R] find and replace characters in a string
Hello, The period is a metacharacter so you have to escape it. The period is escaped with a '\'. In it's turn, '\' is a metacharacter so it needs to be escaped. Hence the double'\\'. x <- "LOI ." gsub("\\.", "(%)", x) Hope this helps, Rui Barradas Em 27-03-2013 16:09, Shane Carey escreveu: Hi, I have a string of text as follows "LOI ." How do I replace the dot with "(%)" gsub(".","(%)",LOI .) gives "(%)(%)(%)(%)(%)" Thanks __ R-help@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.
Re: [R] find and replace characters in a string
txt<- "LOI ." gsub("[.]","%",txt) #[1] "LOI %" A.K. From: Shane Carey To: r-help@r-project.org Sent: Wednesday, March 27, 2013 12:09 PM Subject: [R] find and replace characters in a string Hi, I have a string of text as follows "LOI ." How do I replace the dot with "(%)" gsub(".","(%)",LOI .) gives "(%)(%)(%)(%)(%)" Thanks -- Shane [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.
[R] find and replace characters in a string
Hi, I have a string of text as follows "LOI ." How do I replace the dot with "(%)" gsub(".","(%)",LOI .) gives "(%)(%)(%)(%)(%)" Thanks -- Shane [[alternative HTML version deleted]] __ R-help@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.
Re: [R] find and replace
HI, You can also try this: Students1<-data.frame(ID=c(101,201,303,304),Name=c("Andrew","John","Julie","Monica"),Fav_Place=c("Phoenix AZ","San Francisco","California/New York","New York")) gsubfun<-function(pattern,replacement,x, ...){ for(i in seq_along(pattern)) x<-gsub(pattern[i],replacement[i],x,...) x } toreplace<-c("Phoenix","New York") replacedwith<-c("Tucson","New York City") Students1$Fav_Place<-gsubfun(toreplace,replacedwith,Students1$Fav_Place) Students1 # ID Name Fav_Place #1 101 Andrew Tucson AZ #2 201 John San Francisco #3 303 Julie California/New York City #4 304 Monica New York City A.K. - Original Message - From: Sapana Lohani To: R help Cc: Sent: Monday, August 27, 2012 7:47 PM Subject: [R] find and replace Hi, My data frame (Students) is ID Name Fav_Place 101 Andrew Phoenix AZ 201 John San Francisco 303 JulieCalifornia / New York 304 Monica New York How can I replace Phoenix with Tucson & New York with New York City in the df? Thanks [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.
Re: [R] find and replace
Hi, Try this: dat1<-readLines(textConnection( "ID Name Fav_Place 101 Andrew Phoenix,AZ 201 John SanFrancisco 303 Julie California/New York 304 Monica New York")) gsub("Phoenix","Tucson",gsub("New York","New York City",dat1)) #[1] "ID Name Fav_Place" "101 Andrew Tucson,AZ" #[3] "201 John SanFrancisco" "303 Julie California/New York City" #[5] "304 Monica New York City" A.K. - Original Message - From: Sapana Lohani To: R help Cc: Sent: Monday, August 27, 2012 7:47 PM Subject: [R] find and replace Hi, My data frame (Students) is ID Name Fav_Place 101 Andrew Phoenix AZ 201 John San Francisco 303 JulieCalifornia / New York 304 Monica New York How can I replace Phoenix with Tucson & New York with New York City in the df? Thanks [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.
Re: [R] find and replace
Hi, Try this: set.seed(1) dat1<-data.frame(A=sample(letters[20:25],replace=TRUE),B=sample(letters[1:6],replace=TRUE),C=c(letters[1:3],letters[3:1]),D=sample(letters[2:7],replace=TRUE),E=sample(letters[21:26],replace=TRUE)) newdat<-list() for(i in 1:ncol(dat1)){ newdat[[i]]<-list() newdat[[i]]<-gsub("x","y",gsub("a","b",dat1[,i]))} newdat1<-data.frame(do.call(cbind,newdat)) newdat1 # X1 X2 X3 X4 X5 #1 u f b f w #2 v d b d y #3 w d c f z #4 y b c d v #5 u b b f y #6 y b b g u A.K. - Original Message - From: Sapana Lohani To: R help Cc: Sent: Monday, August 27, 2012 4:19 PM Subject: [R] find and replace I have 5 (A,B,C,D,E) columns in my dataframe. I want to replace all "x" with "y" and all "a" with "b" within these 5 columns. Can I do it in one step? Thanks [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.
Re: [R] find and replace
Take a look at gsub() Michael On Aug 27, 2012, at 6:47 PM, Sapana Lohani wrote: > Hi, > > My data frame (Students) is > > ID Name Fav_Place > 101 Andrew� Phoenix AZ > 201 John San Francisco > 303 JulieCalifornia / New York > 304 Monica� New York > > How can I replace Phoenix with Tucson & New York with New York City in the df? > > Thanks >[[alternative HTML version deleted]] > > __ > R-help@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. __ R-help@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.
[R] find and replace
Hi, My data frame (Students) is ID Name Fav_Place 101 Andrew Phoenix AZ 201 John San Francisco 303 JulieCalifornia / New York 304 Monica New York How can I replace Phoenix with Tucson & New York with New York City in the df? Thanks [[alternative HTML version deleted]] __ R-help@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.
Re: [R] find and replace
I am making the assumption that all the columns are character and not factors: for (i in c("A", "B", "C", "D", "E")){ yourdf[[i]] <- ifelse(yourdf[[i]] == 'x' , 'y' , ifelse(yourdf[[i]] == 'a' , 'b' , yourdf[[i]] ) ) } On Mon, Aug 27, 2012 at 4:19 PM, Sapana Lohani wrote: > I have 5 (A,B,C,D,E) columns in my dataframe. I want to replace all "x" with > "y" and all "a" with "b" within these 5 columns. Can I do it in one step? > > Thanks > [[alternative HTML version deleted]] > > __ > R-help@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. -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. __ R-help@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.
[R] find and replace
I have 5 (A,B,C,D,E) columns in my dataframe. I want to replace all "x" with "y" and all "a" with "b" within these 5 columns. Can I do it in one step? Thanks [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Find and install old package to R 2.11 in UNIX
On 17.08.2012 02:42, David Winsemius wrote: On Aug 16, 2012, at 4:33 PM, Kevin Goulding wrote: Hi, I'm having trouble installing and using the 'foreign' package in R on a UNIX machine. sessionInfo() R version 2.11.1 (2010-05-31) sparc-sun-solaris2.10 locale: [1] C attached base packages: [1] stats graphics grDevices utils datasets methods base Is there a way to find and install an old 'foreign' package that works with my machine? Does such a method generalize to finding other out-of-date packages for my machine? Yes, there is a general method. Install from archived package as source. http://cran.r-project.org/src/contrib/Archive Right, but then foreign is a recommended package and is shipped with released versions of R, hence should be available already. Best, Uwe Ligges __ R-help@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.
Re: [R] Find and install old package to R 2.11 in UNIX
On Aug 16, 2012, at 4:33 PM, Kevin Goulding wrote: Hi, I'm having trouble installing and using the 'foreign' package in R on a UNIX machine. sessionInfo() R version 2.11.1 (2010-05-31) sparc-sun-solaris2.10 locale: [1] C attached base packages: [1] stats graphics grDevices utils datasets methods base Is there a way to find and install an old 'foreign' package that works with my machine? Does such a method generalize to finding other out-of-date packages for my machine? Yes, there is a general method. Install from archived package as source. http://cran.r-project.org/src/contrib/Archive -- David Winsemius, MD Alameda, CA, USA __ R-help@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.
[R] Find and install old package to R 2.11 in UNIX
Hi, I'm having trouble installing and using the 'foreign' package in R on a UNIX machine. > sessionInfo() R version 2.11.1 (2010-05-31) sparc-sun-solaris2.10 locale: [1] C attached base packages: [1] stats graphics grDevices utils datasets methods base Is there a way to find and install an old 'foreign' package that works with my machine? Does such a method generalize to finding other out-of-date packages for my machine? Thanks! Kevin [[alternative HTML version deleted]] __ R-help@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.
Re: [R] find and replace string
You've been given a workable solution already, but here's a one-liner: > x <- c('sta_+1+0_field2ndtry_$01.cfg' , > 'sta_+B+0_field2ndtry_$01.cfg' , 'sta_+1+0_field2ndtry_$01.cfg' , > 'sta_+9+0_field2ndtry_$01.cfg') > sapply(1:length(x), function(i)gsub("\\+(.*)\\+.", paste("\\+\\1\\+", i, > sep=""), x[i])) [1] "sta_+1+1_field2ndtry_$01.cfg" "sta_+B+2_field2ndtry_$01.cfg" [3] "sta_+1+3_field2ndtry_$01.cfg" "sta_+9+4_field2ndtry_$01.cfg" Sarah, fan of regular expressions On Fri, Dec 2, 2011 at 6:30 AM, Alaios wrote: > Dear all, > I would like to search in a string for the second occurrence of a symbol and > replace the symbol after it > > For example my strings look like > > sta_+1+0_field2ndtry_$01.cfg > > I want to find the digit that comes after the second +, in that case is zero > and then over a loop create the strings below > > sta_+1+0_field2ndtry_$01.cfg > > sta_+1+1_field2ndtry_$01.cfg > > sta_+1+2_field2ndtry_$01.cfg > > sta_+1+3_field2ndtry_$01.cfg > > and so on.. > I have already tried strsplit but this will make things more complex... > > Could you please help me with that? > > B.R > Alex > -- Sarah Goslee http://www.functionaldiversity.org __ R-help@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.
Re: [R] find and replace string
You are too good :) Thanks a lot have a nice weekend B.R Alex From: jim holtman Cc: "R-help@r-project.org" Sent: Friday, December 2, 2011 1:51 PM Subject: Re: [R] find and replace string try this: > x <- c('sta_+1+0_field2ndtry_$01.cfg' + , 'sta_+1+0_field2ndtry_$01.cfg' + , 'sta_+1-0_field2ndtry_$01.cfg' + , 'sta_+1+0_field2ndtry_$01.cfg' + ) > # find matching fields > values <- grep("[^+]*\\+[^+]*\\+0", x, value = TRUE) > # split into two pieces > splitValues <- sub("([^+]*\\+[^+]*\\+)0(.*)", "\\1^\\2", values) > for (i in splitValues){ + for (j in 0:3){ + print(sub("\\^", j, i)) + } + } [1] "sta_+1+0_field2ndtry_$01.cfg" [1] "sta_+1+1_field2ndtry_$01.cfg" [1] "sta_+1+2_field2ndtry_$01.cfg" [1] "sta_+1+3_field2ndtry_$01.cfg" [1] "sta_+1+0_field2ndtry_$01.cfg" [1] "sta_+1+1_field2ndtry_$01.cfg" [1] "sta_+1+2_field2ndtry_$01.cfg" [1] "sta_+1+3_field2ndtry_$01.cfg" [1] "sta_+1+0_field2ndtry_$01.cfg" [1] "sta_+1+1_field2ndtry_$01.cfg" [1] "sta_+1+2_field2ndtry_$01.cfg" [1] "sta_+1+3_field2ndtry_$01.cfg" > Dear all, > I would like to search in a string for the second occurrence of a symbol and > replace the symbol after it > > For example my strings look like > > sta_+1+0_field2ndtry_$01.cfg > > I want to find the digit that comes after the second +, in that case is zero > and then over a loop create the strings below > > sta_+1+0_field2ndtry_$01.cfg > > sta_+1+1_field2ndtry_$01.cfg > > sta_+1+2_field2ndtry_$01.cfg > > sta_+1+3_field2ndtry_$01.cfg > > and so on.. > I have already tried strsplit but this will make things more complex... > > Could you please help me with that? > > B.R > Alex > > [[alternative HTML version deleted]] > > __ > R-help@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. -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. [[alternative HTML version deleted]] __ R-help@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.
Re: [R] find and replace string
If the length of the fists part is constant (the "sta_+1+" part) the you can use substr() On 2 December 2011 13:30, Alaios wrote: > Dear all, > I would like to search in a string for the second occurrence of a symbol and > replace the symbol after it > > For example my strings look like > > sta_+1+0_field2ndtry_$01.cfg > > I want to find the digit that comes after the second +, in that case is zero > and then over a loop create the strings below > > sta_+1+0_field2ndtry_$01.cfg > > sta_+1+1_field2ndtry_$01.cfg > > sta_+1+2_field2ndtry_$01.cfg > > sta_+1+3_field2ndtry_$01.cfg > > and so on.. > I have already tried strsplit but this will make things more complex... > > Could you please help me with that? > > B.R > Alex > -- Christiaan Pauw Nova Institute www.nova.org.za __ R-help@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.
Re: [R] find and replace string
try this: > x <- c('sta_+1+0_field2ndtry_$01.cfg' + , 'sta_+1+0_field2ndtry_$01.cfg' + , 'sta_+1-0_field2ndtry_$01.cfg' + , 'sta_+1+0_field2ndtry_$01.cfg' + ) > # find matching fields > values <- grep("[^+]*\\+[^+]*\\+0", x, value = TRUE) > # split into two pieces > splitValues <- sub("([^+]*\\+[^+]*\\+)0(.*)", "\\1^\\2", values) > for (i in splitValues){ + for (j in 0:3){ + print(sub("\\^", j, i)) + } + } [1] "sta_+1+0_field2ndtry_$01.cfg" [1] "sta_+1+1_field2ndtry_$01.cfg" [1] "sta_+1+2_field2ndtry_$01.cfg" [1] "sta_+1+3_field2ndtry_$01.cfg" [1] "sta_+1+0_field2ndtry_$01.cfg" [1] "sta_+1+1_field2ndtry_$01.cfg" [1] "sta_+1+2_field2ndtry_$01.cfg" [1] "sta_+1+3_field2ndtry_$01.cfg" [1] "sta_+1+0_field2ndtry_$01.cfg" [1] "sta_+1+1_field2ndtry_$01.cfg" [1] "sta_+1+2_field2ndtry_$01.cfg" [1] "sta_+1+3_field2ndtry_$01.cfg" On Fri, Dec 2, 2011 at 6:30 AM, Alaios wrote: > Dear all, > I would like to search in a string for the second occurrence of a symbol and > replace the symbol after it > > For example my strings look like > > sta_+1+0_field2ndtry_$01.cfg > > I want to find the digit that comes after the second +, in that case is zero > and then over a loop create the strings below > > sta_+1+0_field2ndtry_$01.cfg > > sta_+1+1_field2ndtry_$01.cfg > > sta_+1+2_field2ndtry_$01.cfg > > sta_+1+3_field2ndtry_$01.cfg > > and so on.. > I have already tried strsplit but this will make things more complex... > > Could you please help me with that? > > B.R > Alex > > [[alternative HTML version deleted]] > > __ > R-help@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. -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. __ R-help@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.
[R] find and replace string
Dear all, I would like to search in a string for the second occurrence of a symbol and replace the symbol after it For example my strings look like sta_+1+0_field2ndtry_$01.cfg I want to find the digit that comes after the second +, in that case is zero and then over a loop create the strings below sta_+1+0_field2ndtry_$01.cfg sta_+1+1_field2ndtry_$01.cfg sta_+1+2_field2ndtry_$01.cfg sta_+1+3_field2ndtry_$01.cfg and so on.. I have already tried strsplit but this will make things more complex... Could you please help me with that? B.R Alex [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Find and replace all the elements in a data frame
You may write as this: for (i in 1:nrow(x)){ for (j in 1:ncol(x)){ if (!is.na(x[i, j])) { if(x[i, j] == 'A') {x2[i, j] <- 'A/A'} else{ if(x[i, j] == 'T') {x2[i, j] <- 'T/T'} else{ if(x[i, j] == 'G') {x2[i, j] <- 'G/G'} else{ if(x[i, j] == 'G') {x2[i, j] <- 'G/G'} else{x2[i, j] <- NA} } } } } } } On Thu, Feb 17, 2011 at 11:54 AM, Josh B wrote: > Hi all, > > I'm having a problem once again, trying to do something very simple. > Consider > the following data frame: > > x <- read.table(textConnection("locus1 locus2 locus3 > A T C > A T NA > T C C > A T G"), header = TRUE) > closeAllConnections() > > I am trying to make a new data frame, replacing "A" with "A/A", "T" with > "T/T", > "G" with "G/G", and "C" with "C/C." Note also the presence of an "NA" > (missing > data) in the data frame, which should be carried over to the new data > frame. > > Here is what I am trying, which fails miserably: > > x2 <- data.frame(matrix(nrow = nrow(x), ncol = ncol(x))) > > for (i in 1:nrow(x)){ >for (j in 1:ncol(x)){ >if(x[i, j] == 'A') {x2[i, j] <- 'A/A'} else{ >if(x[i, j] == 'T') {x2[i, j] <- 'T/T'} else{ > if(x[i, j] == 'G') {x2[i, j] <- 'G/G'} else{ >if(x[i, j] == 'G') {x2[i, j] <- 'G/G'} else{x2[i, j] <- > NA} >} > } > } >} > } > > I get the following error message: > Error in if (x[i, j] == "A") { : missing value where TRUE/FALSE needed > > So what am I doing wrong? If you can provide me with specific code that > fixes > the problem and gets the job done, that would be the most useful. > > > Thanks very much in advance for your help! > > Sincerely, > --- > Josh Banta, Ph.D > Center for Genomics and Systems Biology > New York University > 100 Washington Square East > New York, NY 10003 > Tel: (212) 998-8465 > http://plantevolutionaryecology.org > > > >[[alternative HTML version deleted]] > > __ > R-help@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. > -- Gong-Yi Liao Department of Statistics University of Connecticut 215 Glenbrook Road U4120 Storrs, CT 06269-4120 860-486-9478 [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Find and replace all the elements in a data frame
Hi, You could use car::recode to change the levels of the factors, library(car) transform(x, locus1 = recode(locus1, "'A' = 'A/A' ; else = 'T/T'"), locus2 = recode(locus2, "'T'='T/T' ; 'C' = 'C/C'"), locus3 = recode(locus3, "'C'='C/C' ; 'G' = 'G/G'")) HTH, baptiste On 17 February 2011 17:54, Josh B wrote: > Hi all, > > I'm having a problem once again, trying to do something very simple. Consider > the following data frame: > > x <- read.table(textConnection("locus1 locus2 locus3 > A T C > A T NA > T C C > A T G"), header = TRUE) > closeAllConnections() > > I am trying to make a new data frame, replacing "A" with "A/A", "T" with > "T/T", > "G" with "G/G", and "C" with "C/C." Note also the presence of an "NA" (missing > data) in the data frame, which should be carried over to the new data frame. > > Here is what I am trying, which fails miserably: > > x2 <- data.frame(matrix(nrow = nrow(x), ncol = ncol(x))) > > for (i in 1:nrow(x)){ > for (j in 1:ncol(x)){ > if(x[i, j] == 'A') {x2[i, j] <- 'A/A'} else{ > if(x[i, j] == 'T') {x2[i, j] <- 'T/T'} else{ > if(x[i, j] == 'G') {x2[i, j] <- 'G/G'} else{ > if(x[i, j] == 'G') {x2[i, j] <- 'G/G'} else{x2[i, j] <- NA} > } > } > } > } > } > > I get the following error message: > Error in if (x[i, j] == "A") { : missing value where TRUE/FALSE needed > > So what am I doing wrong? If you can provide me with specific code that fixes > the problem and gets the job done, that would be the most useful. > > > Thanks very much in advance for your help! > > Sincerely, > --- > Josh Banta, Ph.D > Center for Genomics and Systems Biology > New York University > 100 Washington Square East > New York, NY 10003 > Tel: (212) 998-8465 > http://plantevolutionaryecology.org > > > > [[alternative HTML version deleted]] > > __ > R-help@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. > __ R-help@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.
Re: [R] Find and replace all the elements in a data frame
Try this: xNew <- as.data.frame(mapply(paste, x, x, sep = "/")) xNew[is.na(x)] <- NA xNew On Thu, Feb 17, 2011 at 2:54 PM, Josh B wrote: > Hi all, > > I'm having a problem once again, trying to do something very simple. > Consider > the following data frame: > > x <- read.table(textConnection("locus1 locus2 locus3 > A T C > A T NA > T C C > A T G"), header = TRUE) > closeAllConnections() > > I am trying to make a new data frame, replacing "A" with "A/A", "T" with > "T/T", > "G" with "G/G", and "C" with "C/C." Note also the presence of an "NA" > (missing > data) in the data frame, which should be carried over to the new data > frame. > > Here is what I am trying, which fails miserably: > > x2 <- data.frame(matrix(nrow = nrow(x), ncol = ncol(x))) > > for (i in 1:nrow(x)){ >for (j in 1:ncol(x)){ >if(x[i, j] == 'A') {x2[i, j] <- 'A/A'} else{ >if(x[i, j] == 'T') {x2[i, j] <- 'T/T'} else{ > if(x[i, j] == 'G') {x2[i, j] <- 'G/G'} else{ >if(x[i, j] == 'G') {x2[i, j] <- 'G/G'} else{x2[i, j] <- > NA} >} > } > } >} > } > > I get the following error message: > Error in if (x[i, j] == "A") { : missing value where TRUE/FALSE needed > > So what am I doing wrong? If you can provide me with specific code that > fixes > the problem and gets the job done, that would be the most useful. > > > Thanks very much in advance for your help! > > Sincerely, > --- > Josh Banta, Ph.D > Center for Genomics and Systems Biology > New York University > 100 Washington Square East > New York, NY 10003 > Tel: (212) 998-8465 > http://plantevolutionaryecology.org > > > >[[alternative HTML version deleted]] > > __ > R-help@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. > -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Find and replace all the elements in a data frame
Josh, you've made it far too complicated. Here's one simpler way (note that I changed your read.table statement to make the values NOT factors, since I wouldn't think you want that). > x <- read.table(textConnection("locus1 locus2 locus3 + A T C + A T NA + T C C + A T G"), header = TRUE, as.is=TRUE) > closeAllConnections() > > x2 <- x > x2[x2 == "A"] <- "A/A" > x2[x2 == "T"] <- "T/T" > x2[x2 == "G"] <- "G/G" > x2[x2 == "C"] <- "C/C" > x2 locus1 locus2 locus3 1A/AT/TC/C 2A/AT/T 3T/TC/CC/C 4A/AT/TG/G If you do for some reason want a factor, you'll need to adjust the levels for each column before doing this. Sarah On Thu, Feb 17, 2011 at 11:54 AM, Josh B wrote: > Hi all, > > I'm having a problem once again, trying to do something very simple. Consider > the following data frame: > > x <- read.table(textConnection("locus1 locus2 locus3 > A T C > A T NA > T C C > A T G"), header = TRUE) > closeAllConnections() > > I am trying to make a new data frame, replacing "A" with "A/A", "T" with > "T/T", > "G" with "G/G", and "C" with "C/C." Note also the presence of an "NA" (missing > data) in the data frame, which should be carried over to the new data frame. > > Here is what I am trying, which fails miserably: > > x2 <- data.frame(matrix(nrow = nrow(x), ncol = ncol(x))) > > for (i in 1:nrow(x)){ > for (j in 1:ncol(x)){ > if(x[i, j] == 'A') {x2[i, j] <- 'A/A'} else{ > if(x[i, j] == 'T') {x2[i, j] <- 'T/T'} else{ > if(x[i, j] == 'G') {x2[i, j] <- 'G/G'} else{ > if(x[i, j] == 'G') {x2[i, j] <- 'G/G'} else{x2[i, j] <- NA} > } > } > } > } > } > > I get the following error message: > Error in if (x[i, j] == "A") { : missing value where TRUE/FALSE needed > > So what am I doing wrong? If you can provide me with specific code that fixes > the problem and gets the job done, that would be the most useful. > > > Thanks very much in advance for your help! > > Sincerely, > --- > Josh Banta, Ph.D > Center for Genomics and Systems Biology > New York University > 100 Washington Square East > New York, NY 10003 > Tel: (212) 998-8465 > http://plantevolutionaryecology.org > > > > [[alternative HTML version deleted]] -- Sarah Goslee http://www.functionaldiversity.org __ R-help@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.
[R] Find and replace all the elements in a data frame
Hi all, I'm having a problem once again, trying to do something very simple. Consider the following data frame: x <- read.table(textConnection("locus1 locus2 locus3 A T C A T NA T C C A T G"), header = TRUE) closeAllConnections() I am trying to make a new data frame, replacing "A" with "A/A", "T" with "T/T", "G" with "G/G", and "C" with "C/C." Note also the presence of an "NA" (missing data) in the data frame, which should be carried over to the new data frame. Here is what I am trying, which fails miserably: x2 <- data.frame(matrix(nrow = nrow(x), ncol = ncol(x))) for (i in 1:nrow(x)){ for (j in 1:ncol(x)){ if(x[i, j] == 'A') {x2[i, j] <- 'A/A'} else{ if(x[i, j] == 'T') {x2[i, j] <- 'T/T'} else{ if(x[i, j] == 'G') {x2[i, j] <- 'G/G'} else{ if(x[i, j] == 'G') {x2[i, j] <- 'G/G'} else{x2[i, j] <- NA} } } } } } I get the following error message: Error in if (x[i, j] == "A") { : missing value where TRUE/FALSE needed So what am I doing wrong? If you can provide me with specific code that fixes the problem and gets the job done, that would be the most useful. Thanks very much in advance for your help! Sincerely, --- Josh Banta, Ph.D Center for Genomics and Systems Biology New York University 100 Washington Square East New York, NY 10003 Tel: (212) 998-8465 http://plantevolutionaryecology.org [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Find and remove elemnts of a data frame
Thanks a lot worked fine Regards --- On Thu, 1/6/11, John Kane wrote: From: John Kane Subject: Re: [R] Find and remove elemnts of a data frame To: r-help@r-project.org, "Alaios" Date: Thursday, January 6, 2011, 3:27 PM With dataframe xx (naming a data.frame as data.frame is a bit dicey subset(xx, xx[,4]> -45) --- On Thu, 1/6/11, Alaios wrote: > From: Alaios > Subject: [R] Find and remove elemnts of a data frame > To: r-help@r-project.org > Received: Thursday, January 6, 2011, 10:07 AM > Dear all, > I have a data frame that is created like that > data.frame(x=CRX[-1],y=CRY[-1],z=CRagent[[1]]$sr) > > the output looks like > 45 116 162 -30.89105988567164 > 46 128 79 -42.66296679571184 > 47 180 195 -30.45626175641315 > 48 114 83 -45.26843476475688 > 49 118 73 -46.85389245327003 > > > How can I select only the rows that their third column is > higher that -45? > This will return the following > 116 162 -30.89105988567164 > 128 79 -42.66296679571184 > 180 195 -30.45626175641315 > > I would like to thank you in advance for your help > > Best Regards > Alex > > > > > [[alternative HTML version deleted]] > > > -Inline Attachment Follows- > > __ > R-help@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. > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Find and remove elemnts of a data frame
With dataframe xx (naming a data.frame as data.frame is a bit dicey subset(xx, xx[,4]> -45) --- On Thu, 1/6/11, Alaios wrote: > From: Alaios > Subject: [R] Find and remove elemnts of a data frame > To: r-help@r-project.org > Received: Thursday, January 6, 2011, 10:07 AM > Dear all, > I have a data frame that is created like that > data.frame(x=CRX[-1],y=CRY[-1],z=CRagent[[1]]$sr) > > the output looks like > 45 116 162 -30.89105988567164 > 46 128 79 -42.66296679571184 > 47 180 195 -30.45626175641315 > 48 114 83 -45.26843476475688 > 49 118 73 -46.85389245327003 > > > How can I select only the rows that their third column is > higher that -45? > This will return the following > 116 162 -30.89105988567164 > 128 79 -42.66296679571184 > 180 195 -30.45626175641315 > > I would like to thank you in advance for your help > > Best Regards > Alex > > > > > [[alternative HTML version deleted]] > > > -Inline Attachment Follows- > > __ > R-help@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. > __ R-help@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.
[R] Find and remove elemnts of a data frame
Dear all, I have a data frame that is created like that data.frame(x=CRX[-1],y=CRY[-1],z=CRagent[[1]]$sr) the output looks like 45 116 162 -30.89105988567164 46 128 79 -42.66296679571184 47 180 195 -30.45626175641315 48 114 83 -45.26843476475688 49 118 73 -46.85389245327003 How can I select only the rows that their third column is higher that -45? This will return the following 116 162 -30.89105988567164 128 79 -42.66296679571184 180 195 -30.45626175641315 I would like to thank you in advance for your help Best Regards Alex [[alternative HTML version deleted]] __ R-help@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.