Re: [R] about saving format
I have no clue how Rstudio saves plots, but when I was writing directly to the pdf plot device I had similar problems. Setting useDingbats = TRUE made everything work well. I think it is more prudent - and less clicking here and there - to save plots from within the script. I imagine this works as expected: pdf(useDingbats = TRUE) plot(0, xlab = "Percent (‰)", ylab = "Another Percent (‰)”) dev.off() Best, Ulrik Duncan Murdoch schrieb am So., 6. Aug. 2017, 03:44: > On 05/08/2017 9:10 PM, Bert Gunter wrote: > > If the OP is using RStudio and not using R (i.e. pdf()) directly, it > > sounds like this query should be directed to RStudio support, not > > here. > > Two things: > > First, to Lily: "‰" is the "per mil" symbol, not percent; it's not an > ASCII symbol, which is why there are issues displaying it. If you > really mean percent, use "%" and you likely won't have problems. > > And to confirm what Bert said: this does work in R even if you use > dev.copy() to copy from the regular R graphics device to pdf, but not > from the RStudioGD to pdf. So this really is an RStudio bug. > > Duncan Murdoch > > > > > 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, Aug 5, 2017 at 5:54 PM, Ismail SEZEN > wrote: > >> > >>> On 6 Aug 2017, at 03:47, lily li wrote: > >>> > >>> In the lower right panel of R-studio interface, there is the "Export" > button. I saved as PDF from there directly, rather than using functions > >>> > >>> On Sat, Aug 5, 2017 at 6:18 PM, Ismail SEZEN > wrote: > >>> > On 6 Aug 2017, at 03:01, lily li wrote: > > I am using the plot() function, but have a problem. When saving as pdf > format, the ‰ sign in the x-axis label becomes (...) sign. I prefer > to save > in pdf, as this format has a higher resolution than jpeg or other > picture > formats. Could anyone tell me how to do then? Thanks. > >>> > >>> Please, share minimal example. which function do you use to save the > plot as pdf? ‘pdf' or ‘cairo_pdf’ or something else? > >> > >> I used the code below, and used export button to save as pdf. It saves > as expected. Please, minimize your code as below and share. So I can try on > my machine. > >> > >> plot(0, xlab = "Percent (‰)", ylab = "Another Percent (‰)”) > >> __ > >> 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] SPSS R Factor v2.4.2
> On Aug 5, 2017, at 7:02 PM, Gavin Brown wrote: > > I am not an R-Head, hence I use nice utilities that integrate R into SPSS > I have SPSS v24, R3.20 and R3.40 > I have run IBM SPSS R Integration which requires linking to R3.20 > I have installed R Factor v2.4.2 > This package requires 'polycor' library > Unfortunately, 'polycor' does not exist in R3.20 That's not really correct. There is an archived version that should be compatible with your out-of-date version of R. See: ftp://cran.r-project.org/pub/R/src/contrib/Archive/polycor/ Good luck; David. > > DATASET ACTIVATE DataSet1. > *M�rio Basto, Jos� Manuel Pereira, IPCA > *Required: SPSS 21 and R Integration Plugin > *R Packages required: psych, polycor, GPArotation, nFactors, corpcor, ICS, > R.utils. > set printback off. > Error in library(polycor) : there is no package called 'polycor' > > This means the very good utility does not run in the way I have installed > both R and SPSS > Does anyone know what I've done wrong and how to overcome this? > > Prof. Gavin T L Brown, PhD > Director Quantitative Data Analysis and Research Unit > Faculty of Education & Social Work > The University of Auckland > Tel: +64 9 3737599 ext. 48602 > Mob: +64 22 108 7253 > Honorary Professor, Dept. of Curriculum & Instruction, Education University > of Hong Kong > Affiliated Professor, Dept. of Applied Educational Sciences, University of > Umea, Sweden > > New: > https://www.routledge.com/Assessment-of-Student-Achievement/Brown/p/book/9781138061866 > > > [[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.
Re: [R] SPSS R Factor v2.4.2
Well, you could download and install it into R! https://cran.r-project.org/web/packages/polycor/index.html However, as this seems to be about how SPSS integrates R, maybe you should post on their support list for help. 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, Aug 5, 2017 at 7:02 PM, Gavin Brown wrote: > I am not an R-Head, hence I use nice utilities that integrate R into SPSS > I have SPSS v24, R3.20 and R3.40 > I have run IBM SPSS R Integration which requires linking to R3.20 > I have installed R Factor v2.4.2 > This package requires 'polycor' library > Unfortunately, 'polycor' does not exist in R3.20 > > DATASET ACTIVATE DataSet1. > *Mário Basto, José Manuel Pereira, IPCA > *Required: SPSS 21 and R Integration Plugin > *R Packages required: psych, polycor, GPArotation, nFactors, corpcor, ICS, > R.utils. > set printback off. > Error in library(polycor) : there is no package called 'polycor' > > This means the very good utility does not run in the way I have installed > both R and SPSS > Does anyone know what I've done wrong and how to overcome this? > > Prof. Gavin T L Brown, PhD > Director Quantitative Data Analysis and Research Unit > Faculty of Education & Social Work > The University of Auckland > Tel: +64 9 3737599 ext. 48602 > Mob: +64 22 108 7253 > Honorary Professor, Dept. of Curriculum & Instruction, Education University > of Hong Kong > Affiliated Professor, Dept. of Applied Educational Sciences, University of > Umea, Sweden > > New: > https://www.routledge.com/Assessment-of-Student-Achievement/Brown/p/book/9781138061866 > > > [[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] SPSS R Factor v2.4.2
I am not an R-Head, hence I use nice utilities that integrate R into SPSS I have SPSS v24, R3.20 and R3.40 I have run IBM SPSS R Integration which requires linking to R3.20 I have installed R Factor v2.4.2 This package requires 'polycor' library Unfortunately, 'polycor' does not exist in R3.20 DATASET ACTIVATE DataSet1. *M�rio Basto, Jos� Manuel Pereira, IPCA *Required: SPSS 21 and R Integration Plugin *R Packages required: psych, polycor, GPArotation, nFactors, corpcor, ICS, R.utils. set printback off. Error in library(polycor) : there is no package called 'polycor' This means the very good utility does not run in the way I have installed both R and SPSS Does anyone know what I've done wrong and how to overcome this? Prof. Gavin T L Brown, PhD Director Quantitative Data Analysis and Research Unit Faculty of Education & Social Work The University of Auckland Tel: +64 9 3737599 ext. 48602 Mob: +64 22 108 7253 Honorary Professor, Dept. of Curriculum & Instruction, Education University of Hong Kong Affiliated Professor, Dept. of Applied Educational Sciences, University of Umea, Sweden New: https://www.routledge.com/Assessment-of-Student-Achievement/Brown/p/book/9781138061866 [[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] about saving format
On 05/08/2017 9:10 PM, Bert Gunter wrote: If the OP is using RStudio and not using R (i.e. pdf()) directly, it sounds like this query should be directed to RStudio support, not here. Two things: First, to Lily: "‰" is the "per mil" symbol, not percent; it's not an ASCII symbol, which is why there are issues displaying it. If you really mean percent, use "%" and you likely won't have problems. And to confirm what Bert said: this does work in R even if you use dev.copy() to copy from the regular R graphics device to pdf, but not from the RStudioGD to pdf. So this really is an RStudio bug. Duncan Murdoch 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, Aug 5, 2017 at 5:54 PM, Ismail SEZEN wrote: On 6 Aug 2017, at 03:47, lily li wrote: In the lower right panel of R-studio interface, there is the "Export" button. I saved as PDF from there directly, rather than using functions On Sat, Aug 5, 2017 at 6:18 PM, Ismail SEZEN wrote: On 6 Aug 2017, at 03:01, lily li wrote: I am using the plot() function, but have a problem. When saving as pdf format, the ‰ sign in the x-axis label becomes (...) sign. I prefer to save in pdf, as this format has a higher resolution than jpeg or other picture formats. Could anyone tell me how to do then? Thanks. Please, share minimal example. which function do you use to save the plot as pdf? ‘pdf' or ‘cairo_pdf’ or something else? I used the code below, and used export button to save as pdf. It saves as expected. Please, minimize your code as below and share. So I can try on my machine. plot(0, xlab = "Percent (‰)", ylab = "Another Percent (‰)”) __ 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] about saving format
If the OP is using RStudio and not using R (i.e. pdf()) directly, it sounds like this query should be directed to RStudio support, not here. 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, Aug 5, 2017 at 5:54 PM, Ismail SEZEN wrote: > >> On 6 Aug 2017, at 03:47, lily li wrote: >> >> In the lower right panel of R-studio interface, there is the "Export" >> button. I saved as PDF from there directly, rather than using functions >> >> On Sat, Aug 5, 2017 at 6:18 PM, Ismail SEZEN wrote: >> >> > On 6 Aug 2017, at 03:01, lily li wrote: >> > >> > I am using the plot() function, but have a problem. When saving as pdf >> > format, the ‰ sign in the x-axis label becomes (...) sign. I prefer to save >> > in pdf, as this format has a higher resolution than jpeg or other picture >> > formats. Could anyone tell me how to do then? Thanks. >> >> Please, share minimal example. which function do you use to save the plot as >> pdf? ‘pdf' or ‘cairo_pdf’ or something else? > > I used the code below, and used export button to save as pdf. It saves as > expected. Please, minimize your code as below and share. So I can try on my > machine. > > plot(0, xlab = "Percent (‰)", ylab = "Another Percent (‰)”) > __ > 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] about saving format
> On 6 Aug 2017, at 03:47, lily li wrote: > > In the lower right panel of R-studio interface, there is the "Export" button. > I saved as PDF from there directly, rather than using functions > > On Sat, Aug 5, 2017 at 6:18 PM, Ismail SEZEN wrote: > > > On 6 Aug 2017, at 03:01, lily li wrote: > > > > I am using the plot() function, but have a problem. When saving as pdf > > format, the ‰ sign in the x-axis label becomes (...) sign. I prefer to save > > in pdf, as this format has a higher resolution than jpeg or other picture > > formats. Could anyone tell me how to do then? Thanks. > > Please, share minimal example. which function do you use to save the plot as > pdf? ‘pdf' or ‘cairo_pdf’ or something else? I used the code below, and used export button to save as pdf. It saves as expected. Please, minimize your code as below and share. So I can try on my machine. plot(0, xlab = "Percent (‰)", ylab = "Another Percent (‰)”) __ 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] about saving format
In the lower right panel of R-studio interface, there is the "Export" button. I saved as PDF from there directly, rather than using functions On Sat, Aug 5, 2017 at 6:18 PM, Ismail SEZEN wrote: > > > On 6 Aug 2017, at 03:01, lily li wrote: > > > > I am using the plot() function, but have a problem. When saving as pdf > > format, the ‰ sign in the x-axis label becomes (...) sign. I prefer to > save > > in pdf, as this format has a higher resolution than jpeg or other picture > > formats. Could anyone tell me how to do then? Thanks. > > Please, share minimal example. which function do you use to save the plot > as pdf? ‘pdf' or ‘cairo_pdf’ or something else? > > [[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] about saving format
> On 6 Aug 2017, at 03:01, lily li wrote: > > I am using the plot() function, but have a problem. When saving as pdf > format, the ‰ sign in the x-axis label becomes (...) sign. I prefer to save > in pdf, as this format has a higher resolution than jpeg or other picture > formats. Could anyone tell me how to do then? Thanks. Please, share minimal example. which function do you use to save the plot as pdf? ‘pdf' or ‘cairo_pdf’ or something else? __ 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] about saving format
Hi R users, I am using the plot() function, but have a problem. When saving as pdf format, the ‰ sign in the x-axis label becomes (...) sign. I prefer to save in pdf, as this format has a higher resolution than jpeg or other picture formats. Could anyone tell me how to do then? Thanks. [[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] Error in setwd("dir") :
Hello, Inline. Em 05-08-2017 15:55, ? ? escreveu: Hello, I got some of the R source code and not being able to Run it If you have the source code, why can't you change it and replace that "dir" with yours, as returned by getwd()? Hope this helps, Rui Barradas in RStudio. I get the error: Error in setwd("dir") : cannot change working directory I have gone through forums but nothing seemed relevant to my issue. What bugs me the most is the ("dir") that the error shows, is of those who wrote the source code and not mine(it still sees the directory of their computer, even though, I have changed all the directories). When I write getwd(), it dispalys my "dir". Does anyone have any ideas on what's going on here? Thank you -- Greg Lyukshin [[alternative HTML version deleted]] [[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.
Re: [R] Error in setwd("dir") :
On 05/08/2017 10:55 AM, ? ? wrote: Hello, I got some of the R source code and not being able to Run it in RStudio. I get the error: Error in setwd("dir") : cannot change working directory I have gone through forums but nothing seemed relevant to my issue. What bugs me the most is the ("dir") that the error shows, is of those who wrote the source code and not mine(it still sees the directory of their computer, even though, I have changed all the directories). When I write getwd(), it dispalys my "dir". Does anyone have any ideas on what's going on here? Try sourcing it with "echo=TRUE", so you can see which line is causing the problem. Presumably some part of the code still references a particular directory. If you are loading binary objects (e.g. using load() or readRDS()), they may trigger code to run that may still have a directory coded. But more likely you've just missed fixing something. Duncan Murdoch __ 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] Error in setwd("dir") :
Did you put the directory in quotes with forward slashes? Please show command used. Sent from my iPhone > On Aug 5, 2017, at 1:10 PM, ? ? wrote: > > Hello, I got some of the R source code and not being able to Run it in > RStudio. > I get the error: > > Error in setwd("dir") : > cannot change working directory > > > I have gone through forums but nothing seemed relevant to my issue. > What bugs me the most is the ("dir") that the error shows, is of those who > wrote the source code and not mine(it still sees the directory of their > computer, even though, I have changed all the directories). > When I write getwd(), it dispalys my "dir". > Does anyone have any ideas on what's going on here? > Thank you > > > -- > Greg Lyukshin > >[[alternative HTML version deleted]] > >[[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] Error in setwd("dir") :
Hello, I got some of the R source code and not being able to Run it in RStudio. I get the error: Error in setwd("dir") : cannot change working directory I have gone through forums but nothing seemed relevant to my issue. What bugs me the most is the ("dir") that the error shows, is of those who wrote the source code and not mine(it still sees the directory of their computer, even though, I have changed all the directories). When I write getwd(), it dispalys my "dir". Does anyone have any ideas on what's going on here? Thank you -- Greg Lyukshin [[alternative HTML version deleted]] [[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] by() subset by factor gives unexpected results
Putting answer here for future posterity. Didn't send to R-help initially because I wasn't sure what OP wanted. I guessed right. Sorry for confusion in thread. GUESSING THAT YOU WANT IS BELOW #=== i <- data.frame(x=c(1,2,3), y=c(0,0,0), B=c("red","blue","blue"), stringsAsFactors = FALSE) j <- data.frame(x=c(1,2,3), y=c(1,1,1), B=c('red','blue','green'), stringsAsFactors = FALSE) plot(0, 0, type="n", xlim=c(0,4), ylim=c(0,1)) points(i$x, i$y, col = i$B) points(j$x, j$y, col = j$B) On Sat, Aug 5, 2017 at 5:59 AM, Myles English wrote: > > The answer was (thanks to Mark Leeds) to do with the use of a factor > instead of a vector. > > on [2017-08-05] at 08:57 Myles English writes: > > > I am having trouble understanding how the 'by' function works. Using > > this bit of code: > > > > i <- data.frame(x=c(1,2,3), y=c(0,0,0), B=c("red","blue","blue")) > > j <- data.frame(x=c(1,2,3), y=c(1,1,1), B=c('red','blue','green')) > > The use of I() prevents conversion to a factor: > > i <- data.frame(x=c(1,2,3), y=c(0,0,0), B=I(c("red","blue","blue"))) > j <- data.frame(x=c(1,2,3), y=c(1,1,1), B=I(c('red','blue','green'))) > > > plot(0, 0, type="n", xlim=c(0,4), ylim=c(0,1)) > > by(i, i$B, function(s){ points(s$x, s$y, col=s$B) }) > > by(j, j$B, function(s){ points(s$x, s$y, col=s$B) }) > > > > I would have expected the point at (1,1) to be coloured red. When > > plotted, this row is indeed red: > > > >> i[1,] > > x y B > > 1 1 0 red > > > > however, this next point is green on the plot even though I would like > > it to be red: > > > >> j[1,] > > x y B > > 1 1 1 red > > > > How can I achieve that? > > > > Myles > > [[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] by() subset by factor gives unexpected results
... and, of course, by() should not be used at all for this sort of thing in practice, as the "col" argument can be a vector. See ?plot.default if you you were not aware of this already. j <- data.frame(x=c(1,2,3), y=c(1,1,1), B=c("red","blue","green"), stringsAsFactors = FALSE) with(j,plot(x,y, col=B, xlim=c(0,4), ylim=c(0,1.2))) 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, Aug 5, 2017 at 2:59 AM, Myles English wrote: > > The answer was (thanks to Mark Leeds) to do with the use of a factor > instead of a vector. > > on [2017-08-05] at 08:57 Myles English writes: > >> I am having trouble understanding how the 'by' function works. Using >> this bit of code: >> >> i <- data.frame(x=c(1,2,3), y=c(0,0,0), B=c("red","blue","blue")) >> j <- data.frame(x=c(1,2,3), y=c(1,1,1), B=c('red','blue','green')) > > The use of I() prevents conversion to a factor: > > i <- data.frame(x=c(1,2,3), y=c(0,0,0), B=I(c("red","blue","blue"))) > j <- data.frame(x=c(1,2,3), y=c(1,1,1), B=I(c('red','blue','green'))) > >> plot(0, 0, type="n", xlim=c(0,4), ylim=c(0,1)) >> by(i, i$B, function(s){ points(s$x, s$y, col=s$B) }) >> by(j, j$B, function(s){ points(s$x, s$y, col=s$B) }) >> >> I would have expected the point at (1,1) to be coloured red. When >> plotted, this row is indeed red: >> >>> i[1,] >> x y B >> 1 1 0 red >> >> however, this next point is green on the plot even though I would like >> it to be red: >> >>> j[1,] >> x y B >> 1 1 1 red >> >> How can I achieve that? >> >> Myles > > __ > 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] Nested for loop
Hi! Thanks for taking the time to read this. The code below creates a graph that takes 100 samples that are between 5% and 15% of the population (400). What I'd like to do, however, is add two other sections to the graph. It would look something like this: from 1-100 samples take 100 samples that are between 5% and 15% of the population (400). From 101-200 take 100 samples that are between 5% and 15% of the population (800). From 201-300 take 100 samples that are between 5% and 15% of the population (300). I assume this would require a nested for loop. Does anyone have advice as to how to do this? Thanks for your time. Kirsten ## Mark-Recapture ## Estimate popoulation from repeated sampling ## Population size N <- 400 N ## Vector labeling each item in the population pop <- c(1:N) pop ## Lower and upper bounds of sample size lower.bound <- round(x = .05 * N, digits = 0) lower.bound ## Smallest possible sample size upper.bound <- round(x = .15 * N, digits = 0) upper.bound ## Largest possible sample size ## Length of sample size interval length.ss.interval <- length(c(lower.bound:upper.bound)) length.ss.interval ## total possible sample sizes, ranging form lower.bound to upper.bound ## Determine a sample size randomly (not a global variable...simply for test purposes) ## Between lower and upper bounds set previously ## Give equal weight to each possible sample size in this interval sample(x = c(lower.bound:upper.bound), size = 1, prob = c(rep(1/length.ss.interval, length.ss.interval))) ## Specify number of samples to take n.samples <- 100 ## Initiate empty matrix ## 1st column is population (item 1 thorugh item 400) ## 2nd through nth column are all rounds of sampling dat <- matrix(data = NA, nrow = length(pop), ncol = n.samples + 1) dat[,1] <- pop dat ## Take samples of random sizes ## Record results in columns 2 through n ## 1 = sampled (marked) ## 0 = not sampled (not marked) for(i in 2:ncol(dat)) { a.sample <- sample(x = pop, size = sample(x = c(lower.bound:upper.bound), size = 1, prob = c(rep(1/length.ss.interval, length.ss.interval))), replace = FALSE) dat[,i] <- dat[,1] %in% a.sample } ## How large was each sample size? apply(X = dat, MARGIN = 2, FUN = sum) ## 1st element is irrelevant ## 2nd element through nth element: sample size for each of the 100 samples ## At this point, all computations can be done using dat ## Create Schnabel dataframe using dat ## Google the Schnabel formula schnabel.comp <- data.frame(sample = 1:n.samples, n.sampled = apply(X = dat, MARGIN = 2, FUN = sum)[2:length(apply(X = dat, MARGIN = 2, FUN = sum))] ) ## First column: which sample, 1-100 ## Second column: number selected in that sample ## How many items were previously sampled? ## For 1st sample, it's 0 ## For 2nd sample, code is different than for remaning samples n.prev.sampled <- c(0, rep(NA, n.samples-1)) n.prev.sampled n.prev.sampled[2] <- sum(ifelse(test = dat[,3] == 1 & dat[,2] == 1, yes = 1, no = 0)) n.prev.sampled for(i in 4:ncol(dat)) { n.prev.sampled[i-1] <- sum(ifelse(test = dat[,i] == 1 & rowSums(dat[,2:(i-1)]) > 0, yes = 1, no = 0)) } schnabel.comp$n.prev.sampled <- n.prev.sampled ## n.newly.sampled: in each sample, how many items were newly sampled? ## i.e., never seen before? schnabel.comp$n.newly.sampled <- with(schnabel.comp, n.sampled - n.prev.sampled) ## cum.sampled: how many total items have you seen? schnabel.comp$cum.sampled <- c(0, cumsum(schnabel.comp$n.newly.sampled)[2:n.samples-1]) ## numerator of schnabel formula schnabel.comp$numerator <- with(schnabel.comp, n.sampled * cum.sampled) ## denominator of schnable formula is n.prev.sampled ## pop.estimate -- after each sample (starting with 2nd -- need at least two samples) schnabel.comp$pop.estimate <- NA for(i in 1:length(schnabel.comp$pop.estimate)) { schnabel.comp$pop.estimate[i] <- sum(schnabel.comp$numerator[1:i]) / sum(schnabel.comp$n.prev.sampled[1:i]) } ## Plot population estimate after each sample if (!require("ggplot2")) {install.packages("ggplot2"); require("ggplot2")} if (!require("scales")) {install.packages("scales"); require("scales")} small.sample.dat <- schnabel.comp small.sample <- ggplot(data = small.sample.dat, mapping = aes(x = sample, y = pop.estimate)) + geom_point(size = 2) + geom_line() + geom_hline(yintercept = N, col = "red", lwd = 1) + coord_cartesian(xlim = c(0:100), ylim = c(300:500)) + scale_x_continuous(breaks = pretty_breaks(11)) + scale_y_continuous(breaks = pretty_breaks(11)) + labs(x = "\nSample", y = "Population estimate\n", title = "Sampl
Re: [R] Ussing assign
Hi Jesus, You probably want: assign(paste0("model",i),lm(y~x,data=get(paste0("variable",i Jim On Sat, Aug 5, 2017 at 7:01 PM, Jesús Para Fernández wrote: > Hi there, > > > Imagine I want to use a variable created with assign,but automatically. I > have an issue with that. What I do is: > > > #create in a loop the variables > > > for(i in 1:5){ > > > assign(paste0("variable",i),data.frame(x=rnorm(100),y=rnorm(100)) > > } > > > Now, if I want to do create the five models, I do not know how to set this > created data.frames > > > I have tried: > > > for (i in 1:5){ > > > assign(paste0("model",i),lm(y~x,data=paste0("variable",i))) > > > } > > > but it does not working. > > Error in eval(predvars, data, env) : > invalid 'envir' argument of type 'character' > > > Can you please help me to find the correct way? > > > Thanks! > Jesús > > > > > > > > > > [[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.
Re: [R] by() subset by factor gives unexpected results
The answer was (thanks to Mark Leeds) to do with the use of a factor instead of a vector. on [2017-08-05] at 08:57 Myles English writes: > I am having trouble understanding how the 'by' function works. Using > this bit of code: > > i <- data.frame(x=c(1,2,3), y=c(0,0,0), B=c("red","blue","blue")) > j <- data.frame(x=c(1,2,3), y=c(1,1,1), B=c('red','blue','green')) The use of I() prevents conversion to a factor: i <- data.frame(x=c(1,2,3), y=c(0,0,0), B=I(c("red","blue","blue"))) j <- data.frame(x=c(1,2,3), y=c(1,1,1), B=I(c('red','blue','green'))) > plot(0, 0, type="n", xlim=c(0,4), ylim=c(0,1)) > by(i, i$B, function(s){ points(s$x, s$y, col=s$B) }) > by(j, j$B, function(s){ points(s$x, s$y, col=s$B) }) > > I would have expected the point at (1,1) to be coloured red. When > plotted, this row is indeed red: > >> i[1,] > x y B > 1 1 0 red > > however, this next point is green on the plot even though I would like > it to be red: > >> j[1,] > x y B > 1 1 1 red > > How can I achieve that? > > Myles __ 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] by() subset by factor gives unexpected results
I am having trouble understanding how the 'by' function works. Using this bit of code: i <- data.frame(x=c(1,2,3), y=c(0,0,0), B=c("red","blue","blue")) j <- data.frame(x=c(1,2,3), y=c(1,1,1), B=c('red','blue','green')) plot(0, 0, type="n", xlim=c(0,4), ylim=c(0,1)) by(i, i$B, function(s){ points(s$x, s$y, col=s$B) }) by(j, j$B, function(s){ points(s$x, s$y, col=s$B) }) I would have expected the point at (1,1) to be coloured red. When plotted, this row is indeed red: > i[1,] x y B 1 1 0 red however, this next point is green on the plot even though I would like it to be red: > j[1,] x y B 1 1 1 red How can I achieve that? Myles __ 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.