Re: [R] "effects" package with "lme4"
Dear Axel, > On May 15, 2020, at 7:39 PM, Axel Urbiz wrote: > > Dear John, > > Thank you for your response. > > My apologies as I’m only recently getting exposed to mixed-model. My > understanding, is that the model specified below also has random intercepts > and slope, as they vary by Subject. `coef(fm1)` shows this. I was looking to > plot the fitted splines by Subject. > > Sorry if my interpretation is incorrect. There's no need to apologize. The functions in the effects package compute and graph fixed effects for mixed models. You could compute and graph the BLUP for the fitted spline for each subject (see below) but it's not what the effects package does. I think that the following does what you want: snip -- subjects <- levels(sleepstudy$Subject) fits <- matrix(0, length(subjects), 10) rownames(fits) <- subjects for (subject in subjects){ fits[subject, ] <- predict(fm1, newdata=data.frame(Subject=subject, Days=0:9)) } plot(c(0, 10), range(fits), type="n", xlab="Days", ylab="Reaction") for (subject in subjects) lines(0:9, fits[subject, ]) snip -- Best, John > > Best, > Axel. > > > >> On May 15, 2020, at 5:51 PM, Fox, John wrote: >> >> Dear Axel, >> >> There only one fixed effect in the model, ns(Days, 3), so I don't know what >> you expected. >> >> Best, >> John >> -- >> John Fox, Professor Emeritus >> McMaster University >> Hamilton, Ontario, Canada >> Web: socialsciences.mcmaster.ca/jfox/ >> >> >> >>> -Original Message- >>> From: Axel Urbiz >>> Sent: Friday, May 15, 2020 5:33 PM >>> To: Fox, John ; R-help@r-project.org >>> Subject: "effects" package with "lme4" >>> >>> Hello John and others, >>> >>> I’d appreciate your help as I’m trying to plot the effect of predictor >>> “Days” on Reaction by Subject. I’m only getting one plot in the example >>> below. >>> >>> ### Start example >>> >>> library(lme4) >>> library(splines) >>> data("sleepstudy") >>> >>> fm1 <- lmer(Reaction ~ ns(Days, 3) + (ns(Days, 3) | Subject), sleepstudy) >>> coef(fm1) >>> plot(allEffects(fm1)) >>> >>> ### End example >>> >>> Thanks, >>> Axel. > __ 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] "effects" package with "lme4"
Dear John, Thank you for your response. My apologies as I’m only recently getting exposed to mixed-model. My understanding, is that the model specified below also has random intercepts and slope, as they vary by Subject. `coef(fm1)` shows this. I was looking to plot the fitted splines by Subject. Sorry if my interpretation is incorrect. Best, Axel. > On May 15, 2020, at 5:51 PM, Fox, John wrote: > > Dear Axel, > > There only one fixed effect in the model, ns(Days, 3), so I don't know what > you expected. > > Best, > John > -- > John Fox, Professor Emeritus > McMaster University > Hamilton, Ontario, Canada > Web: socialsciences.mcmaster.ca/jfox/ > > > >> -Original Message- >> From: Axel Urbiz >> Sent: Friday, May 15, 2020 5:33 PM >> To: Fox, John ; R-help@r-project.org >> Subject: "effects" package with "lme4" >> >> Hello John and others, >> >> I’d appreciate your help as I’m trying to plot the effect of predictor >> “Days” on Reaction by Subject. I’m only getting one plot in the example >> below. >> >> ### Start example >> >> library(lme4) >> library(splines) >> data("sleepstudy") >> >> fm1 <- lmer(Reaction ~ ns(Days, 3) + (ns(Days, 3) | Subject), sleepstudy) >> coef(fm1) >> plot(allEffects(fm1)) >> >> ### End example >> >> Thanks, >> Axel. __ 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] addScales package announcement
In case anyone here is interested (this appeared on the r-packages list, but it seems that posts there are no longer forwarded here): addScales is a small package now on CRAN that modifies trellis objects created using lattice graphics by adding horizontal and/or vertical reference lines to provide visual scaling information. This is mostly useful for multi-panel plots that use the relation = 'free' option in their 'scales' argument list. Examples show when and how this might aid data visualization. Please feel free to contact me with any suggestions for improvement. Bert Gunter [[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] how to extract strings in any column and in any row that start with
Hi Rui, thank you so much that is exactly what I needed! Cheers, Ana On Fri, May 15, 2020 at 5:12 PM Rui Barradas wrote: > > Hello, > > I have tried several options and with large dataframes this one was the > fastest (in my tests, of the ones I have tried). > > > s1 <- sapply(tot, function(x) grep('^E10', x, value = TRUE)) > > > Then unlist(s1). > A close second (15% slower) was > > > s2 <- tot[sapply(tot, function(x) grepl('^E10', x))] > > > grep/unlist was 3.7 times slower: > > > grep("^E10", unlist(tot), value = TRUE) > > > Hope this helps, > > Rui Barradas > > Às 20:24 de 15/05/20, Ana Marija escreveu: > > Hello, > > > > this command was running for more than 2 hours > > grep("E10",tot,value=T) > > and no output > > > > and this command > > df1 <- tot %>% filter_all(any_vars(grepl( '^E10', .))) > > > > gave me a subset (a data frame) of tot where ^E10 > > > > what I need is just a vector or all values in tot which start with E10. > > > > Thanks > > Ana > > > > On Fri, May 15, 2020 at 12:13 PM Jeff Newmiller > > wrote: > >> > >> Read about regular expressions... they are extremely useful. > >> > >> df1 <- tot %>% filter_all(any_vars(grepl( '^E10', .))) > >> > >> It is bad form not to put spaces around the <- assignment. > >> > >> > >> On May 15, 2020 10:00:04 AM PDT, Ana Marija > >> wrote: > >>> Hello, > >>> > >>> I have a data frame: > >>> > dim(tot) > >>> [1] 502536 1093 > >>> > >>> How would I extract from it all strings that start with E10? > >>> > >>> I know how to extract all rows that contain with E10 > >>> df0<-tot %>% filter_all(any_vars(. %in% c('E10'))) > dim(df0) > >>> [1] 5105 1093 > >>> > >>> but I just need a vector of strings that start with E10... > >>> it would look something like this: > >>> > >>> [1] "E102" "E109" "E108" "E103" "E104" "E105" "E101" "E106" "E107" > >>> > >>> Thanks > >>> Ana > >>> > >>> __ > >>> 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. > >> > >> -- > >> Sent from my phone. Please excuse my brevity. > > > > __ > > 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] how to extract strings in any column and in any row that start with
Hello, I have tried several options and with large dataframes this one was the fastest (in my tests, of the ones I have tried). s1 <- sapply(tot, function(x) grep('^E10', x, value = TRUE)) Then unlist(s1). A close second (15% slower) was s2 <- tot[sapply(tot, function(x) grepl('^E10', x))] grep/unlist was 3.7 times slower: grep("^E10", unlist(tot), value = TRUE) Hope this helps, Rui Barradas Às 20:24 de 15/05/20, Ana Marija escreveu: Hello, this command was running for more than 2 hours grep("E10",tot,value=T) and no output and this command df1 <- tot %>% filter_all(any_vars(grepl( '^E10', .))) gave me a subset (a data frame) of tot where ^E10 what I need is just a vector or all values in tot which start with E10. Thanks Ana On Fri, May 15, 2020 at 12:13 PM Jeff Newmiller wrote: Read about regular expressions... they are extremely useful. df1 <- tot %>% filter_all(any_vars(grepl( '^E10', .))) It is bad form not to put spaces around the <- assignment. On May 15, 2020 10:00:04 AM PDT, Ana Marija wrote: Hello, I have a data frame: dim(tot) [1] 502536 1093 How would I extract from it all strings that start with E10? I know how to extract all rows that contain with E10 df0<-tot %>% filter_all(any_vars(. %in% c('E10'))) dim(df0) [1] 5105 1093 but I just need a vector of strings that start with E10... it would look something like this: [1] "E102" "E109" "E108" "E103" "E104" "E105" "E101" "E106" "E107" Thanks Ana __ 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. -- Sent from my phone. Please excuse my brevity. __ 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] "effects" package with "lme4"
Dear Axel, There only one fixed effect in the model, ns(Days, 3), so I don't know what you expected. Best, John -- John Fox, Professor Emeritus McMaster University Hamilton, Ontario, Canada Web: socialsciences.mcmaster.ca/jfox/ > -Original Message- > From: Axel Urbiz > Sent: Friday, May 15, 2020 5:33 PM > To: Fox, John ; R-help@r-project.org > Subject: "effects" package with "lme4" > > Hello John and others, > > I’d appreciate your help as I’m trying to plot the effect of predictor > “Days” on Reaction by Subject. I’m only getting one plot in the example > below. > > ### Start example > > library(lme4) > library(splines) > data("sleepstudy") > > fm1 <- lmer(Reaction ~ ns(Days, 3) + (ns(Days, 3) | Subject), sleepstudy) > coef(fm1) > plot(allEffects(fm1)) > > ### End example > > Thanks, > Axel. __ 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] how to extract strings in any column and in any row that start with
> How would I extract from it all strings that start with E10? Hi Ana, Here's a simple solution: x <- c ("P24601", "E101", "E102", "3.141593", "E101", "xE101", "e103", " E104 ") x [substring (x, 1, 3) == "E10"] You' will need to replace x with another *character vector*. (As touched on earlier, a data.frame may cause some problems). Here's some variations: unique (x [substring (x, 1, 3) == "E10"]) y <- toupper (x) y [substring (y, 1, 3) == "E10"] y <- trimws (x) y [substring (y, 1, 3) == "E10"] __ 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] "effects" package with "lme4"
Hello John and others, I’d appreciate your help as I’m trying to plot the effect of predictor “Days” on Reaction by Subject. I’m only getting one plot in the example below. ### Start example library(lme4) library(splines) data("sleepstudy") fm1 <- lmer(Reaction ~ ns(Days, 3) + (ns(Days, 3) | Subject), sleepstudy) coef(fm1) plot(allEffects(fm1)) ### End example Thanks, Axel. __ 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] how to extract strings in any column and in any row that start with
This is almost certainly not the most efficient way: tot <- data.frame(v1 = paste0(LETTERS[seq(1:5)],seq(1:10)), v2 = paste0(LETTERS[seq(1:5)],seq(from = 101, to=110, by = 1)), v3 = paste0(LETTERS[seq(1:5)],seq(from = 111, to=120, by = 1)), v4 = paste0(LETTERS[seq(1:5)],seq(from = 121, to=130, by = 1)), v5 = paste0(LETTERS[seq(1:5)],seq(from = 131, to=140, by = 1)), v6 = paste0(LETTERS[seq(1:5)],seq(from = 101, to=110, by = 1)) ) # set a variable to hold the result myResult <- NULL # iterate through each variable for (v in 1:length(tot[1,])) { thisResult <- as.character(tot[grepl ('^E10', tot[,v]),v]) myResult <- c(myResult, thisResult) } myResult <- unique( myResult ) === Indeed as I wrote this Jeff has popped along with unlist! Using my example above: unique ( as.character( unlist (tot) )[grepl ('^E10', as.character( unlist (tot) ) )] ) does what you wanted (you may not need the as.characters if you are on R 4.o, or if your df has chars rather than factors. On 2020-05-15 21:34, Jeff Newmiller wrote: If you want to treat your data frame as if it were a vector, then convert it to a vector before you give it to grep. unlist(tot) On May 15, 2020 12:24:17 PM PDT, Ana Marija wrote: Hello, this command was running for more than 2 hours grep("E10",tot,value=T) and no output and this command df1 <- tot %>% filter_all(any_vars(grepl( '^E10', .))) gave me a subset (a data frame) of tot where ^E10 what I need is just a vector or all values in tot which start with E10. Thanks Ana On Fri, May 15, 2020 at 12:13 PM Jeff Newmiller wrote: Read about regular expressions... they are extremely useful. df1 <- tot %>% filter_all(any_vars(grepl( '^E10', .))) It is bad form not to put spaces around the <- assignment. On May 15, 2020 10:00:04 AM PDT, Ana Marija wrote: >Hello, > >I have a data frame: > >> dim(tot) >[1] 502536 1093 > >How would I extract from it all strings that start with E10? > >I know how to extract all rows that contain with E10 >df0<-tot %>% filter_all(any_vars(. %in% c('E10'))) >> dim(df0) >[1] 5105 1093 > >but I just need a vector of strings that start with E10... >it would look something like this: > >[1] "E102" "E109" "E108" "E103" "E104" "E105" "E101" "E106" "E107" > >Thanks >Ana > >__ >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. -- Sent from my phone. Please excuse my brevity. __ 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] how to extract strings in any column and in any row that start with
If you want to treat your data frame as if it were a vector, then convert it to a vector before you give it to grep. unlist(tot) On May 15, 2020 12:24:17 PM PDT, Ana Marija wrote: >Hello, > >this command was running for more than 2 hours >grep("E10",tot,value=T) >and no output > >and this command >df1 <- tot %>% filter_all(any_vars(grepl( '^E10', .))) > >gave me a subset (a data frame) of tot where ^E10 > >what I need is just a vector or all values in tot which start with E10. > >Thanks >Ana > >On Fri, May 15, 2020 at 12:13 PM Jeff Newmiller > wrote: >> >> Read about regular expressions... they are extremely useful. >> >> df1 <- tot %>% filter_all(any_vars(grepl( '^E10', .))) >> >> It is bad form not to put spaces around the <- assignment. >> >> >> On May 15, 2020 10:00:04 AM PDT, Ana Marija > wrote: >> >Hello, >> > >> >I have a data frame: >> > >> >> dim(tot) >> >[1] 502536 1093 >> > >> >How would I extract from it all strings that start with E10? >> > >> >I know how to extract all rows that contain with E10 >> >df0<-tot %>% filter_all(any_vars(. %in% c('E10'))) >> >> dim(df0) >> >[1] 5105 1093 >> > >> >but I just need a vector of strings that start with E10... >> >it would look something like this: >> > >> >[1] "E102" "E109" "E108" "E103" "E104" "E105" "E101" "E106" "E107" >> > >> >Thanks >> >Ana >> > >> >__ >> >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. >> >> -- >> Sent from my phone. Please excuse my brevity. -- Sent from my phone. Please excuse my brevity. __ 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] how to extract strings in any column and in any row that start with
Hello, this command was running for more than 2 hours grep("E10",tot,value=T) and no output and this command df1 <- tot %>% filter_all(any_vars(grepl( '^E10', .))) gave me a subset (a data frame) of tot where ^E10 what I need is just a vector or all values in tot which start with E10. Thanks Ana On Fri, May 15, 2020 at 12:13 PM Jeff Newmiller wrote: > > Read about regular expressions... they are extremely useful. > > df1 <- tot %>% filter_all(any_vars(grepl( '^E10', .))) > > It is bad form not to put spaces around the <- assignment. > > > On May 15, 2020 10:00:04 AM PDT, Ana Marija > wrote: > >Hello, > > > >I have a data frame: > > > >> dim(tot) > >[1] 502536 1093 > > > >How would I extract from it all strings that start with E10? > > > >I know how to extract all rows that contain with E10 > >df0<-tot %>% filter_all(any_vars(. %in% c('E10'))) > >> dim(df0) > >[1] 5105 1093 > > > >but I just need a vector of strings that start with E10... > >it would look something like this: > > > >[1] "E102" "E109" "E108" "E103" "E104" "E105" "E101" "E106" "E107" > > > >Thanks > >Ana > > > >__ > >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. > > -- > Sent from my phone. Please excuse my brevity. __ 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] [R-pkgs] Package "roptions"
Thanks all, My package "roptions" is now available on CRAN. My package contains a collection of tools to develop options strategies, value option contracts using the Black-Scholes-Merten option pricing model and calculate the option Greeks. For more information see Hull, John C. "Options, Futures, and Other Derivatives" (1997, ISBN:0-13-601589-1). Fischer Black, Myron Scholes (1973) "The Pricing of Options and Corporate Liabilities" Download using: install.packages("roptions") https://cran.r-project.org/package=roptions Regards, Anurag Agrawal Bachelor of Science in Finance, Narsee Monjee Institute of Management Studies. ___ Disclaimer: The content of this message is confidential. If you have received it by mistake, please inform us by an email reply and then delete the message. It is forbidden to copy, forward, or in any way reveal the contents of this message to anyone. The integrity and security of this email cannot be guaranteed over the Internet. Therefore, the sender will not be held liable for any damage caused by the message. [[alternative HTML version deleted]] ___ R-packages mailing list r-packa...@r-project.org https://stat.ethz.ch/mailman/listinfo/r-packages __ 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] how to extract strings in any column and in any row that start with
Read about regular expressions... they are extremely useful. df1 <- tot %>% filter_all(any_vars(grepl( '^E10', .))) It is bad form not to put spaces around the <- assignment. On May 15, 2020 10:00:04 AM PDT, Ana Marija wrote: >Hello, > >I have a data frame: > >> dim(tot) >[1] 502536 1093 > >How would I extract from it all strings that start with E10? > >I know how to extract all rows that contain with E10 >df0<-tot %>% filter_all(any_vars(. %in% c('E10'))) >> dim(df0) >[1] 5105 1093 > >but I just need a vector of strings that start with E10... >it would look something like this: > >[1] "E102" "E109" "E108" "E103" "E104" "E105" "E101" "E106" "E107" > >Thanks >Ana > >__ >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. -- Sent from my phone. Please excuse my brevity. __ 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] how to extract strings in any column and in any row that start with
Hello, I have a data frame: > dim(tot) [1] 502536 1093 How would I extract from it all strings that start with E10? I know how to extract all rows that contain with E10 df0<-tot %>% filter_all(any_vars(. %in% c('E10'))) > dim(df0) [1] 5105 1093 but I just need a vector of strings that start with E10... it would look something like this: [1] "E102" "E109" "E108" "E103" "E104" "E105" "E101" "E106" "E107" Thanks Ana __ 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] sort
Hello, Here is a dplyr solution. arrange() sorts by name and desc(ddate) and top_n keeps the first 2 after grouping by ddate. Then it's a matter of being careful with diff() in the summarise instruction. library(dplyr) DF1 %>% mutate(ddate = as.Date(ddate)) %>% arrange(name, desc(ddate)) %>% group_by(name) %>% top_n(2) %>% summarise(diff = ifelse(n() > 1, diff(rev(ddate)), 0)) #Selecting by ddate ## A tibble: 3 x 2 # name diff # #1 A76 #2 B 305 #3 c 0 Hope this helps, Rui Barradas Às 03:58 de 15/05/20, Val escreveu: HI All, I have a sample of data frame DF1<-read.table(text="name ddate A 2019-10-28 A 2018-01-25 A 2020-01-12 A 2017-10-20 B 2020-11-20 B 2019-10-20 B 2017-05-20 B 2020-01-20 c 2009-10-01 ",header=TRUE) 1. I want sort by name and ddate on decreasing order and the output should like as follow A 2020-01-12 A 2019-01-12 A 2018-01-25 A 2017-10-20 B 2020-11-21 B 2020-11-01 B 2019-10-20 B 2017-05-20 c 2009-10-01 2. Take the top two rows by group( names) and the out put should like A 2020-01-12 A 2019-01-12 B 2020-11-21 B 2020-11-01 c 2009-10-01 3. Within each group (name) get the date difference between the first and second rows dates. If a group has only one row then the difference should be 0 The final out put is Name diff A 365 B 20 C 0 Here is my attempt and have an issue at the sorting DF1$DTime <- as.POSIXct(DF1$ddate , format = "%Y-%m-%d") DF2 <- DF1[order(DF1$name, ((as.Date(DF1$DTime, decreasing = TRUE, ] not working Any help? 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.
Re: [R] Date from text
On Fri, 15 May 2020, Poizot Emmanuel writes: > Dear all, > > I've a data frame with a column "Date": > > [1] 11-1993 11-1993 11-1993 11-1993 11-1993 11-1993 11-1996 11-1996 11-1996 > [10] 11-1996 11-1996 11-1996 02-1998 02-1998 02-1998 02-1998 02-1998 02-1998 > [19] 11-1998 11-1998 11-1998 11-1998 11-1998 11-1998 10-2001 10-2001 10-2001 > [28] 10-2001 10-2001 10-2001 02-2003 02-2003 02-2003 02-2003 02-2003 02-2003 > [37] 11-2004 11-2004 11-2004 11-2004 11-2004 11-2004 11-2005 11-2005 11-2005 > [46] 11-2005 11-2005 11-2005 11-2007 11-2007 11-2007 11-2007 11-2007 11-2007 > [55] 10-2008 10-2008 10-2008 10-2008 10-2008 10-2008 03-2009 03-2009 03-2009 > [64] 03-2009 03-2009 03-2009 10-2012 10-2012 10-2012 10-2012 10-2012 10-2012 > [73] 12-2017 12-2017 12-2017 12-2017 12-2017 12-2017 12-2018 12-2018 12-2018 > [82] 12-2018 12-2018 12-2018 > > I want to convert that into real dates: > as.POSIXct(Date, format="%m-%Y") always return "NA" values. > Where am I wrong ? > > regards If you really want a date, I'd suggest 'as.Date'. The help for ?as.Date says: "If the date string does not specify the date completely, the returned answer may be system-specific." So perhaps try something like as.Date(paste0("01-", "11-1993"), format = "%d-%m-%Y") ## [1] "1993-11-01" Or look at 'yearmon' in package 'zoo': library("zoo") as.yearmon("11-1993", format = "%m-%Y") ## [1] "Nov 1993" -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net __ 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] Date from text
Hi, For the usual R text to date conversions, you need a complete date. Since you are missing the day of the month in your source text, you would need to impute that part before making the conversion. Also, since you don't appear to need to worry about time of day, just use as.Date(), instead of as.POSIXct(). In this case, use ?sub to add the day of the month to the source text, for which I will impute the 15th, and then make the conversion: ## Single value for now x <- "11-1993" > sub("-", "-15-", x) [1] "11-15-1993" > as.Date(sub("-", "-15-", x), format = "%m-%d-%Y") [1] "1993-11-15" Regards, Marc Schwartz > On May 15, 2020, at 5:38 AM, Poizot Emmanuel > wrote: > > Dear all, > > I've a data frame with a column "Date": > > [1] 11-1993 11-1993 11-1993 11-1993 11-1993 11-1993 11-1996 11-1996 11-1996 > [10] 11-1996 11-1996 11-1996 02-1998 02-1998 02-1998 02-1998 02-1998 02-1998 > [19] 11-1998 11-1998 11-1998 11-1998 11-1998 11-1998 10-2001 10-2001 10-2001 > [28] 10-2001 10-2001 10-2001 02-2003 02-2003 02-2003 02-2003 02-2003 02-2003 > [37] 11-2004 11-2004 11-2004 11-2004 11-2004 11-2004 11-2005 11-2005 11-2005 > [46] 11-2005 11-2005 11-2005 11-2007 11-2007 11-2007 11-2007 11-2007 11-2007 > [55] 10-2008 10-2008 10-2008 10-2008 10-2008 10-2008 03-2009 03-2009 03-2009 > [64] 03-2009 03-2009 03-2009 10-2012 10-2012 10-2012 10-2012 10-2012 10-2012 > [73] 12-2017 12-2017 12-2017 12-2017 12-2017 12-2017 12-2018 12-2018 12-2018 > [82] 12-2018 12-2018 12-2018 > > I want to convert that into real dates: > as.POSIXct(Date, format="%m-%Y") always return "NA" values. > Where am I wrong ? > > regards __ 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] Notational derivative in R
Hi Jialin, I was not aware of symengine. I just did a search and found that you were recognized by the 'John Chambers Statistical Software Award' committee for 2020 for your work on the symengine package. Congratulations! On Fri, May 15, 2020 at 1:34 PM Jialin Ma wrote: > Hi, > > There is a new package published on CRAN called symengine (I am the > maintainer). That might be suitable for your need. > > Best, > Jialin > > On Thursday, May 14, 2020 2:00:20 PM EDT Jeff Newmiller wrote: > > FWIW I have found all such tools to require babysitting... and for > > interactive use I prefer wxMaxima and some manual translation to R. > > On May 14, 2020 10:50:56 AM PDT, Eric Berger > wrote: > > >Hi Christofer, > > >Look at https://cran.r-project.org/web/views/NumericalMathematics.html > > >and within that page search for Symbolic Mathematics. It shows two > > >packages: Ryacas and rSymPy. > > >I have no experience with them but they may be a good place to start. > > > > > >HTH, > > >Eric > > > > > > > > >On Thu, May 14, 2020 at 8:43 PM Christofer Bogaso < > > > > > >bogaso.christo...@gmail.com> wrote: > > >> Hi, > > >> > > >> I was wondering if R can perform notational derivative something like > > >> Mathematica does as explained in > > >> https://reference.wolfram.com/language/howto/TakeADerivative.html > > >> > > >> Any pointer will be highly appreciated. > > >> > > >> Thanks, > > >> > > >> __ > > >> 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. > > > > > [[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] Notational derivative in R
Hi, There is a new package published on CRAN called symengine (I am the maintainer). That might be suitable for your need. Best, Jialin On Thursday, May 14, 2020 2:00:20 PM EDT Jeff Newmiller wrote: > FWIW I have found all such tools to require babysitting... and for > interactive use I prefer wxMaxima and some manual translation to R. > On May 14, 2020 10:50:56 AM PDT, Eric Berger wrote: > >Hi Christofer, > >Look at https://cran.r-project.org/web/views/NumericalMathematics.html > >and within that page search for Symbolic Mathematics. It shows two > >packages: Ryacas and rSymPy. > >I have no experience with them but they may be a good place to start. > > > >HTH, > >Eric > > > > > >On Thu, May 14, 2020 at 8:43 PM Christofer Bogaso < > > > >bogaso.christo...@gmail.com> wrote: > >> Hi, > >> > >> I was wondering if R can perform notational derivative something like > >> Mathematica does as explained in > >> https://reference.wolfram.com/language/howto/TakeADerivative.html > >> > >> Any pointer will be highly appreciated. > >> > >> Thanks, > >> > >> __ > >> 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. __ 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] Date from text
Dear all, I've a data frame with a column "Date": [1] 11-1993 11-1993 11-1993 11-1993 11-1993 11-1993 11-1996 11-1996 11-1996 [10] 11-1996 11-1996 11-1996 02-1998 02-1998 02-1998 02-1998 02-1998 02-1998 [19] 11-1998 11-1998 11-1998 11-1998 11-1998 11-1998 10-2001 10-2001 10-2001 [28] 10-2001 10-2001 10-2001 02-2003 02-2003 02-2003 02-2003 02-2003 02-2003 [37] 11-2004 11-2004 11-2004 11-2004 11-2004 11-2004 11-2005 11-2005 11-2005 [46] 11-2005 11-2005 11-2005 11-2007 11-2007 11-2007 11-2007 11-2007 11-2007 [55] 10-2008 10-2008 10-2008 10-2008 10-2008 10-2008 03-2009 03-2009 03-2009 [64] 03-2009 03-2009 03-2009 10-2012 10-2012 10-2012 10-2012 10-2012 10-2012 [73] 12-2017 12-2017 12-2017 12-2017 12-2017 12-2017 12-2018 12-2018 12-2018 [82] 12-2018 12-2018 12-2018 I want to convert that into real dates: as.POSIXct(Date, format="%m-%Y") always return "NA" values. Where am I wrong ? regards __ 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] How to find a split point in a curve?
Hi Maybe I am wrong but it seems to me that it is cumulative data from recent epidemy in some state. If yes, instead of inventing wheel I would go to canned and proved solution using tools from https://www.repidemicsconsortium.org/ I found useful and enlightening this blog especially first part from February 18th. https://timchurches.github.io/blog/ Cheers Petr > -Original Message- > From: R-help On Behalf Of Luigi Marongiu > Sent: Thursday, May 14, 2020 5:12 PM > To: r-help > Subject: [R] How to find a split point in a curve? > > Dear all, > I am trying to find a turning point in some data. In the initial phase, the > data increases more or less exponentially (thus it is linear in a nat log > transform), then reaches a plateau. I would like to find the point that > marks the end of the exponential phase. > I understand that the function spline can build a curve; is it possible > with it to find the turning points? I have no idea of how to use spline > though. > Here is a working example. > Thank you > > ``` > Y = c(259, 716, 1404, 2173, 3944, 5403, 7140, 9121, > 11220, 13809, 16634, 19869, 23753, 27447, > 30590, 33975, 36627, 39600, 42067, 44082, > 58190, 63280, 65921, 67929, 69977, 71865, > 73614, 74005, 74894, 75717, 76365, 76579, > 77087, 77493, 77926, 78253, 78680, 79253, > 79455, 79580, 79699, 79838, 79981, 80080, > 80124, 80164, 80183, 80207, 80222, 80230, > 80241, 80261, 80261, 80277, 80290, 80303, > 80337, 80376, 80422, 80461, 80539, 80586, > 80653, 80708, 80762, 80807, 80807, 80886, > 80922, 80957, 80988, 81007, 81037, 81076, > 81108, 81108, 81171, 81213, 81259, 81358, > 81466, 81555, 81601, 81647, 81673, 81998, > 82025, 82041, 82053, 82064, 82094, 82104, > 82110, 82122, 82133, 82136, 82142, 82164, > 82168, 82180, 82181, 82184, 82187, 82188, > 82190, 82192, 82193, 82194) > Y = log(Y) > X = 1:length(Y) > plot(Y ~ X, ylab = "Ln(Y)", xlim=c(0,10, main="zoomed in")) > abline(lm(Y[1:3] ~ X[1:3])) > abline(lm(Y[1:5] ~ X[1:5]), lty=2) > text(7, 6, "After third or fifth point, there is deviance", pos=3) > text(2.5, 10, "Solid line: linear model points 1:3", pos =3) > text(2.5, 9, "Dashed line: linear model points 1:5", pos =3) > plot(Y ~ X, ylab = "Ln(Y)", xlim=c(0,10, main="overall")) > abline(lm(Y[1:3] ~ X[1:3])) > ``` > > [[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] gdistance::accCost fails on some cases
Referring to my mail bellow, this situation is not a expected behavior. Do you think that this is because of a fault in my codes or some bug in gdistance? What could be the problem? Thank you for your consideration. -- Forwarded message - From: Deniz Uğur Date: Thu, May 14, 2020, 02:18 Subject: gdistance::accCost fails when nrows is bigger than 90 To: Hello all, It has been only 2 days since I started using R and I was trying movecost library. Before explaining the unexpected behavior I should say that I’m calling R function from Python using rpy2 package. That shouldn’t be an issue because everything works as expected when a matrix of size 100x90 is given. Okay now, basically I’m converting an 2D matrix to RasterLayer with appropriate CRS and extent properties then transfer it to R runtime. A modified version of Tobler’s hiking function is applied then a shortest-path is calculated. This procedure works for a matrix with column size anywhere between 1 and memory’s limit. However if row size of the matrix exceeds 90 then function is halted on gdistance::accCost saying that: "At structural_properties.c:5313 : cannot run Bellman-Ford algorithm, Negative loop detected while calculating shortest paths” It’s very interesting issue because I have explicitly tried …88, 89, 90, 91 and it halted on +90 I’ve created a gist in case you want to try the application on your computer. Also I’ve included a clean version of the R script I was using bellow. Thank you. https://gist.github.com/DenizUgur/01e18edd03321f3d3928c3afb2bd7145 movecost <- function (dtm, origin, destin, time="s") { altDiff <- function(x){x[2] - x[1]} hd <- gdistance::transition(dtm, altDiff, 8, symm=FALSE) slope <- gdistance::geoCorrection(hd) cost_function <- function(x){ ifelse(x[adj] > 0, 1.3 * exp(-3.5 * abs(x[adj] + 0.05)), 5.3 * exp(-3.5 * abs(x[adj] + 0.05))) } adj <- raster::adjacent(dtm, cells=1:ncell(dtm), pairs=TRUE, directions=8) speed <- slope speed[adj] <- cost_function(slope) speed <- speed * 0.278 Conductance <- gdistance::geoCorrection(speed) accum_final <- gdistance::accCost(Conductance, sp::coordinates(origin)) accum_final <- raster::mask(accum_final, dtm) sPath <- gdistance::shortestPath(Conductance, sp::coordinates(origin), sp:: coordinates(destin), output="SpatialLines") sPath$length <- rgeos::gLength(sPath, byid=TRUE) destin$cost <- raster::extract(accum_final, destin) results <- list("accumulated.cost.raster"=accum_final, "LCPs"=sPath, "dest.loc.w.cost"=destin) } [[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.