Re: [R] using if else function to complete a column in data frame
> On Jan 8, 2017, at 9:21 PM, Cacique Samuraiwrote: > > Hello all! > > I´m trying to complete the "movimento" column in dataframe based in > the values of "kmr" column in two sequential lines, as below: > > data example (dput in the end of email): > > IDkmr movimento > 510.700 314.20NA > 110.700 278.74NA > 210.700 278.74NA > 310.700 278.74NA > 410.700 278.74NA > 494 100.700 269.94NA > 500 100.700 278.74NA > 499 100.700 314.20NA > 495 100.700 278.74NA > 498 100.700 278.74NA > 496 100.700 255.40NA > 497 100.700 255.10NA > > Once I have different IDs, I wrote this function: > > move = function (x){ > > for (j in x$ID){ > >for (i in 2:length(x$kmr)-1){ > > if (x$kmr[i+1] < x$kmr[i]) { >x$movimento[i+1] <- "jusante" > } else if (x$kmr[i+1] > x$kmr[i]) { >x$movimento[i+1] <- "montante" > } else { >x$movimento[i+1] <- "parado" > } > >} > > } > > return (x) > } > > Worked pretty well with just one ID, but with many IDs the function > didn´t detach different IDs. > > IDkmr movimento > 510.700 314.20 > 110.700 278.74 jusante > 210.700 278.74parado > 310.700 278.74parado > 410.700 278.74parado > 494 100.700 269.94 jusante <-- this should be The inner loop was not "recognizing" (or more accurately you were not causing the code to account for the fact) that you wanted this to be done within values of ID. Each pass of the outer loop was doing the same process inside the inner loop. > 500 100.700 278.74 montante > 499 100.700 314.20 montante > 495 100.700 278.74 jusante > 498 100.700 278.74parado > 496 100.700 255.40 jusante > 497 100.700 255.10 jusante > > I also tried remove the first If condition and pass this function > using lapply in the splitted original data-frame, but didn´t work as > well. > > Some onde can help? > > Thanks in advanced, > > Raoni > > structure(list(ID = c("10.700", "10.700", "10.700", "10.700", > "10.700", "100.700", "100.700", "100.700", "100.700", "100.700", > "100.700", "100.700"), kmr = c(314.2, 278.74, 278.74, 278.74, > 278.74, 269.94, 278.74, 314.2, 278.74, 278.74, 255.4, 255.1), >movimento = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, >NA)), .Names = c("ID", "kmr", "movimento"), row.names = c(5L, > 1L, 2L, 3L, 4L, 494L, 500L, 499L, 495L, 498L, 496L, 497L), class = > "data.frame") > > -- > Raoni Rosa Rodrigues > Research Associate of Fish Transposition Center CTPeixes > Universidade Federal de Minas Gerais - UFMG > Brasil > rodrigues.ra...@gmail.com > > __ > 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. David Winsemius Alameda, CA, USA __ 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] using if else function to complete a column in data frame
Hi Split option seems to me the most effective and ifelse is in this case not necessary. You can use this function to estimate levels fff <- function(x) sign(diff(x)) test.s <- split(test, test$ID) for (i in 1:length(test.s)) test.s[[i]]$movimento[-1] <- fff(test.s[[i]][,2]) test <- do.call(rbind, test.s) test[,3] <- factor(test[,3], levels=c(-1, 0, 1), labels=c("jusante", "parado", "montante")) test To avoid extension of row names you can use library (plyr) test<-ldply (test.s, data.frame) instead of do.call. I tried assigning factor within for cycle fff<- function(x) factor(sign(diff(x)), levels=c(-1, 0, 1), labels=c("jusante", "parado", "montante")) test.s <- split(test, test$ID) for (i in 1:length(test.s)) test.s[[i]]$movimento[-1] <- fff(test.s[[i]][,2]) test <- do.call(rbind, test.s) test but assigned in this case is not factor but numeric vector, which seems to me strange. Maybe somebody could explain this behaviour Cheers Petr > -Original Message- > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Cacique > Samurai > Sent: Monday, January 9, 2017 6:22 AM > To: R help> Subject: [R] using if else function to complete a column in data frame > > Hello all! > > I´m trying to complete the "movimento" column in dataframe based in the > values of "kmr" column in two sequential lines, as below: > > data example (dput in the end of email): > > IDkmr movimento > 510.700 314.20NA > 110.700 278.74NA > 210.700 278.74NA > 310.700 278.74NA > 410.700 278.74NA > 494 100.700 269.94NA > 500 100.700 278.74NA > 499 100.700 314.20NA > 495 100.700 278.74NA > 498 100.700 278.74NA > 496 100.700 255.40NA > 497 100.700 255.10NA > > Once I have different IDs, I wrote this function: > > move = function (x){ > > for (j in x$ID){ > > for (i in 2:length(x$kmr)-1){ > > if (x$kmr[i+1] < x$kmr[i]) { > x$movimento[i+1] <- "jusante" > } else if (x$kmr[i+1] > x$kmr[i]) { > x$movimento[i+1] <- "montante" > } else { > x$movimento[i+1] <- "parado" > } > > } > > } > > return (x) > } > > Worked pretty well with just one ID, but with many IDs the function didn´t > detach different IDs. > > IDkmr movimento > 510.700 314.20 > 110.700 278.74 jusante > 210.700 278.74parado > 310.700 278.74parado > 410.700 278.74parado > 494 100.700 269.94 jusante <-- this should be > 500 100.700 278.74 montante > 499 100.700 314.20 montante > 495 100.700 278.74 jusante > 498 100.700 278.74parado > 496 100.700 255.40 jusante > 497 100.700 255.10 jusante > > I also tried remove the first If condition and pass this function using > lapply in > the splitted original data-frame, but didn´t work as well. > > Some onde can help? > > Thanks in advanced, > > Raoni > > structure(list(ID = c("10.700", "10.700", "10.700", "10.700", "10.700", > "100.700", "100.700", "100.700", "100.700", "100.700", "100.700", "100.700"), > kmr = c(314.2, 278.74, 278.74, 278.74, 278.74, 269.94, 278.74, 314.2, 278.74, > 278.74, 255.4, 255.1), > movimento = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, > NA)), .Names = c("ID", "kmr", "movimento"), row.names = c(5L, 1L, 2L, 3L, > 4L, 494L, 500L, 499L, 495L, 498L, 496L, 497L), class = "data.frame") > > -- > Raoni Rosa Rodrigues > Research Associate of Fish Transposition Center CTPeixes Universidade > Federal de Minas Gerais - UFMG Brasil rodrigues.ra...@gmail.com > > __ > 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. Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům. Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze svého systému. Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat. Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či zpožděním přenosu e-mailu. V případě, že je tento e-mail součástí obchodního jednání: - vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu. - a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce s dodatkem či odchylkou. - trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným dosažením shody na všech jejích náležitostech. - odesílatel tohoto emailu
[R] R install/libraries
I'd appreciate a tip on (re-)installing libraries for R on my windows machine I have a windows (Windows 7) computer and a recent version of R installed (3.3.2). I discovered that when I install R libraries they install to the "my documents" folder rather than to "program files"This seems to also create problems installing the dependent libraries when a library installs. I'm presuming that R libraries are not installing to "program files" due to some permission/administrator privilege problems. I have tried manually copying files from my documents to program files which didn't seem to completely fix the problem. I also installed "r studio" but that hasn't resolved the problem of libraries not finding other libraries it depends on.A specific example, typically I use Frank Harrel's HMISC, however in my current installation it doesn't find "openssl". installation tips appreciated. Chris Barker, Ph.D. Adjunct Associate Professor of Biostatistics - UIC-SPH and President and Owner Statistical Planning and Analysis Services, Inc. www.barkerstats.com 415 609 7473 skype: barkerstats [[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] gridExtra-arrangeGrob
Hi;The code below used to work on my older version of gridExtra but doesn't work with the new version. Could someonegive me a hint on how to translate this code to the new version of gridExtra code? Thank you beforehand. p1 <- ggplot(iris,aes(Sepal.Length, Petal.Length, colour=Species)) + geom_point() + theme_bw() + theme(legend.position='top') grid.arrange(p1, arrangeGrob(p1,p1,p1, heights=c(0.33, .33,.33), ncol=1), ncol=2) #Create 2 columns with different width using the 'widths' argument in the grid.arrange call grid.arrange(p1, arrangeGrob(p1,p1,p1, heights=c(0.33, .40,.27), ncol=1), ncol=2,widths=c(1.25,0.75)) p <- rectGrob() grid.arrange(p, arrangeGrob(p,p,p, heights=c(0.33, .33,.33), ncol=1), ncol=2) [[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] using if else function to complete a column in data frame
Hello all! I´m trying to complete the "movimento" column in dataframe based in the values of "kmr" column in two sequential lines, as below: data example (dput in the end of email): IDkmr movimento 510.700 314.20NA 110.700 278.74NA 210.700 278.74NA 310.700 278.74NA 410.700 278.74NA 494 100.700 269.94NA 500 100.700 278.74NA 499 100.700 314.20NA 495 100.700 278.74NA 498 100.700 278.74NA 496 100.700 255.40NA 497 100.700 255.10NA Once I have different IDs, I wrote this function: move = function (x){ for (j in x$ID){ for (i in 2:length(x$kmr)-1){ if (x$kmr[i+1] < x$kmr[i]) { x$movimento[i+1] <- "jusante" } else if (x$kmr[i+1] > x$kmr[i]) { x$movimento[i+1] <- "montante" } else { x$movimento[i+1] <- "parado" } } } return (x) } Worked pretty well with just one ID, but with many IDs the function didn´t detach different IDs. IDkmr movimento 510.700 314.20 110.700 278.74 jusante 210.700 278.74parado 310.700 278.74parado 410.700 278.74parado 494 100.700 269.94 jusante <-- this should be 500 100.700 278.74 montante 499 100.700 314.20 montante 495 100.700 278.74 jusante 498 100.700 278.74parado 496 100.700 255.40 jusante 497 100.700 255.10 jusante I also tried remove the first If condition and pass this function using lapply in the splitted original data-frame, but didn´t work as well. Some onde can help? Thanks in advanced, Raoni structure(list(ID = c("10.700", "10.700", "10.700", "10.700", "10.700", "100.700", "100.700", "100.700", "100.700", "100.700", "100.700", "100.700"), kmr = c(314.2, 278.74, 278.74, 278.74, 278.74, 269.94, 278.74, 314.2, 278.74, 278.74, 255.4, 255.1), movimento = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), .Names = c("ID", "kmr", "movimento"), row.names = c(5L, 1L, 2L, 3L, 4L, 494L, 500L, 499L, 495L, 498L, 496L, 497L), class = "data.frame") -- Raoni Rosa Rodrigues Research Associate of Fish Transposition Center CTPeixes Universidade Federal de Minas Gerais - UFMG Brasil rodrigues.ra...@gmail.com __ 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] Linear optimization with quadratic constraints
Small example code to set up the problem? JN On 2017-01-07 06:26 AM, Preetam Pal wrote: > Hi Guys, > Any help with this,please? > Regards, > Preetam > > On Thu, Jan 5, 2017 at 4:09 AM, Preetam Palwrote: > >> Hello guys, >> >> The context is ordinary multivariate regression with k (>1) regressors, >> i.e. *Y = XB + Error*, where >> Y = n X 1 vector of predicted variable, >> X = n X (k + 1) matrix of regressor variables(including ones in the first >> column) >> B = (k+1) vector of coefficients, including intercept. >> >> Say, I have already estimated B as B_hat = (X'X)^(-1) X'Y. >> >> I have to solve the following program: >> >> *minimize f(B) = LB* ( L is a fixed vector 1 X (k+1) ) >> such that: >> *[(B-B_hat)' * X'X * (B-B_hat) ] / [ ( Y - XB_hat)' (Y - XB_hat) ] * is >> less than a given value *c*. >> >> Note that this is a linear optimization program *with respect to B* with >> quadratic constraints. >> >> I don't understand how we can solve this optimization - I was going >> through some online resources, each of which involve manually computing >> gradients of the objective as well as constraint functions - which I want >> to avoid (at least manually doing this). >> >> >> Can you please help with solving this optimization problem? The inputs >> would be: >> >>- X and Y >>- B_hat >>- L >>- c >> >> >> Please let me know if any further information is required - the set-up is >> pretty general. >> >> Regards, >> Preetam >> > > > __ 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.