Re: [R] grep
Thanks! On 8/2/2024 12:28 PM, Rui Barradas wrote: Às 02:10 de 02/08/2024, Steven Yen escreveu: Good Morning. Below I like statement like j<-grep(".r\\b",colnames(mydata),value=TRUE); j with the \\b option which I read long time ago which Ive found useful. Are there more or these options, other than ? grep? Thanks. dstat is just my own descriptive routine. > x [1] "age" "sleep" "primary" "middle" [5] "high" "somewhath" "veryh" "somewhatm" [9] "verym" "somewhatc" "veryc" "somewhatl" [13] "veryl" "village" "married" "social" [17] "agricultural" "communist" "minority" "religious" > colnames(mydata) [1] "depression" "sleep" "female" "village" [5] "agricultural" "married" "communist" "minority" [9] "religious" "social" "no" "primary" [13] "middle" "high" "veryh" "somewhath" [17] "notveryh" "verym" "somewhatm" "notverym" [21] "veryc" "somewhatc" "notveryc" "veryl" [25] "somewhatl" "notveryl" "age" "village.r" [29] "married.r" "social.r" "agricultural.r" "communist.r" [33] "minority.r" "religious.r" "male.r" "education.r" > j<-grep(".r\\b",colnames(mydata),value=TRUE); j [1] "village.r" "married.r" "social.r" "agricultural.r" [5] "communist.r" "minority.r" "religious.r" "male.r" [9] "education.r" > j<-c(x,j); j [1] "age" "sleep" "primary" "middle" [5] "high" "somewhath" "veryh" "somewhatm" [9] "verym" "somewhatc" "veryc" "somewhatl" [13] "veryl" "village" "married" "social" [17] "agricultural" "communist" "minority" "religious" [21] "village.r" "married.r" "social.r" "agricultural.r" [25] "communist.r" "minority.r" "religious.r" "male.r" [29] "education.r" > data<-mydata[j] > cbind( + dstat(subset(data,male.r==1))[,1:2], + dstat(subset(data,male.r==0))[,1:2] + ) Sample statistics (Weighted = FALSE ) Sample statistics (Weighted = FALSE ) Mean Std.dev Mean Std.dev age 6.279 0.841 6.055 0.813 sleep 6.483 1.804 6.087 2.045 primary 0.452 0.498 0.408 0.491 middle 0.287 0.453 0.176 0.381 high 0.171 0.377 0.082 0.275 somewhath 0.522 0.500 0.447 0.497 veryh 0.254 0.435 0.250 0.433 somewhatm 0.419 0.493 0.460 0.498 verym 0.544 0.498 0.411 0.492 somewhatc 0.376 0.484 0.346 0.476 veryc 0.593 0.491 0.615 0.487 somewhatl 0.544 0.498 0.504 0.500 veryl 0.390 0.488 0.389 0.487 village 0.757 0.429 0.752 0.432 married 0.936 0.245 0.906 0.291 social 0.538 0.499 0.528 0.499 agricultural 0.780 0.414 0.826 0.379 communist 0.178 0.383 0.038 0.190 minority 0.071 0.256 0.081 0.273 religious 0.088 0.284 0.102 0.302 village.r 0.243 0.429 0.248 0.432 married.r 0.064 0.245 0.094 0.291 social.r 0.462 0.499 0.472 0.499 agricultural.r 0.220 0.414 0.174 0.379 communist.r 0.822 0.383 0.962 0.190 minority.r 0.929 0.256 0.919 0.273 religious.r 0.912 0.284 0.898 0.302 male.r 1.000 0.000 0.000 0.000 education.r 0.090 0.286 0.334 0.472 > __ 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. Hello, The metacharacters reference is the documentation ?regex. If you want to know whether there are more metacharacters similar to \b, there are \< and \>. low are examples of using them instead of \b. Also, the pattern '.r' does not match a period followed by an 'r', the period matches any
[R] grep
Good Morning. Below I like statement like j<-grep(".r\\b",colnames(mydata),value=TRUE); j with the \\b option which I read long time ago which Ive found useful. Are there more or these options, other than ? grep? Thanks. dstat is just my own descriptive routine. > x [1] "age" "sleep" "primary" "middle" [5] "high" "somewhath" "veryh" "somewhatm" [9] "verym" "somewhatc" "veryc" "somewhatl" [13] "veryl" "village" "married" "social" [17] "agricultural" "communist" "minority" "religious" > colnames(mydata) [1] "depression" "sleep" "female" "village" [5] "agricultural" "married" "communist" "minority" [9] "religious" "social" "no" "primary" [13] "middle" "high" "veryh" "somewhath" [17] "notveryh" "verym" "somewhatm" "notverym" [21] "veryc" "somewhatc" "notveryc" "veryl" [25] "somewhatl" "notveryl" "age" "village.r" [29] "married.r" "social.r" "agricultural.r" "communist.r" [33] "minority.r" "religious.r" "male.r" "education.r" > j<-grep(".r\\b",colnames(mydata),value=TRUE); j [1] "village.r" "married.r" "social.r" "agricultural.r" [5] "communist.r" "minority.r" "religious.r" "male.r" [9] "education.r" > j<-c(x,j); j [1] "age" "sleep" "primary" "middle" [5] "high" "somewhath" "veryh" "somewhatm" [9] "verym" "somewhatc" "veryc" "somewhatl" [13] "veryl" "village" "married" "social" [17] "agricultural" "communist" "minority" "religious" [21] "village.r" "married.r" "social.r" "agricultural.r" [25] "communist.r" "minority.r" "religious.r" "male.r" [29] "education.r" > data<-mydata[j] > cbind( + dstat(subset(data,male.r==1))[,1:2], + dstat(subset(data,male.r==0))[,1:2] + ) Sample statistics (Weighted = FALSE ) Sample statistics (Weighted = FALSE ) Mean Std.dev Mean Std.dev age 6.279 0.841 6.055 0.813 sleep 6.483 1.804 6.087 2.045 primary 0.452 0.498 0.408 0.491 middle 0.287 0.453 0.176 0.381 high 0.171 0.377 0.082 0.275 somewhath 0.522 0.500 0.447 0.497 veryh 0.254 0.435 0.250 0.433 somewhatm 0.419 0.493 0.460 0.498 verym 0.544 0.498 0.411 0.492 somewhatc 0.376 0.484 0.346 0.476 veryc 0.593 0.491 0.615 0.487 somewhatl 0.544 0.498 0.504 0.500 veryl 0.390 0.488 0.389 0.487 village 0.757 0.429 0.752 0.432 married 0.936 0.245 0.906 0.291 social 0.538 0.499 0.528 0.499 agricultural 0.780 0.414 0.826 0.379 communist 0.178 0.383 0.038 0.190 minority 0.071 0.256 0.081 0.273 religious 0.088 0.284 0.102 0.302 village.r 0.243 0.429 0.248 0.432 married.r 0.064 0.245 0.094 0.291 social.r 0.462 0.499 0.472 0.499 agricultural.r 0.220 0.414 0.174 0.379 communist.r 0.822 0.383 0.962 0.190 minority.r 0.929 0.256 0.919 0.273 religious.r 0.912 0.284 0.898 0.302 male.r 1.000 0.000 0.000 0.000 education.r 0.090 0.286 0.334 0.472 > __ 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] grep
Yes. Any of the following worked. The pipe greater than (|>) is neat! Thanks. > v<-goprobit.p$est > names(v) |> grep("somewhat|very", x = _) [1] 6 7 8 9 10 11 12 13 28 29 30 31 32 33 34 35 50 51 52 53 54 55 56 57 > v |> names() |> grep("somewhat|very", x = _) [1] 6 7 8 9 10 11 12 13 28 29 30 31 32 33 34 35 50 51 52 53 54 55 56 57 > which(grepl("very|somewhat",names(v))) [1] 6 7 8 9 10 11 12 13 28 29 30 31 32 33 34 35 50 51 52 53 54 55 56 57 > jj<-grep("very|somewhat",names(v)); jj [1] 6 7 8 9 10 11 12 13 28 29 30 31 32 33 34 35 50 51 52 53 54 55 56 57 > On 7/13/2024 12:31 AM, Rui Barradas wrote: Hello, So any of names(goprobit.p$est) |> grep("somewhat|very", x = _) goprobit.p$est |> names() |> grep("somewhat|very", x = _) should work, right? Hope this helps, Rui Barradas Às 16:57 de 12/07/2024, Steven Yen escreveu: Thanks. First and second of the following worked, but the third (with coef) did not. This may be because I programmed my own estimation program. In short, names(goprobit.p$est) was recognized, but names(coef(goprobit)) was not > which(grepl("very|somewhat",names(goprobit.p$est))) [1] 6 7 8 9 10 11 12 13 28 29 30 31 32 33 34 35 50 51 52 53 54 55 56 57 > jj<-grep("very|somewhat",names(goprobit.p$est)); length(jj) [1] 24 > jj<-grep("very|somewhat",names(coef(goprobit))); length(jj) Error: object 'goprobit' not found On 7/12/2024 11:23 PM, Rui Barradas wrote: Hello,l Though the question is already answered, here is another answer to what is 'x'. The output in the OP is not a lm or glm output but if your regression model was programmed according to recommended practices, there must be a 'coefficients' member in the list or object it returns and the following should work. # this is 'x', a named character vector coef(fit) # fit |> coef() |> names() |> grep("somewhat|very", x = _) Hope this helps, Rui Barradas Às 10:26 de 12/07/2024, Steven Yen escreveu: Thanks. In this case below, what is "x"? I tried rownames(out) which did not work. Sorry. Does this sound like homework to you? On 7/12/2024 5:09 PM, Uwe Ligges wrote: On 12.07.2024 10:54, Steven Yen wrote: Below is part a regression printout. How can I use "grep" to identify rows headed by variables (first column) with a certain label. In this case, I like to find variables containing "somewhath", "veryh", "somewhatm", "verym", "somewhatc", "veryc","somewhatl", "veryl". The result should be an index 6:13 or 6,7,8,9,10,11,12,13. Note that they all contain "somewhat" and "very". Thanks. Sounds like homework? which(grep("very|somewhat", x)) Best, Uwe Ligges est se t p g sig x.1.age 0.0341 0.0138 2.4766 0.0133 -3.8835e-04 ** x.1.sleep -0.1108 0.0059 -18.6277 0. -4.4572e-04 *** x.1.primary -0.0694 0.0289 -2.4002 0.0164 -9.9638e-06 ** x.1.middle -0.2909 0.0356 -8.1657 0. -1.4913e-05 *** x.1.high -0.4267 0.0463 -9.2118 0. -3.6246e-05 *** x.1.somewhath -0.6188 0.0256 -24.1971 0. -3.1337e-05 *** x.1.veryh -0.7580 0.0331 -22.8695 0. -2.9558e-05 *** x.1.somewhatm -0.3413 0.0426 -8.0112 0. -1.8920e-05 *** x.1.verym -0.3813 0.0446 -8.5413 0. -4.4029e-05 *** x.1.somewhatc -0.3101 0.0649 -4.7783 0. -1.4353e-05 *** x.1.veryc -0.2977 0.0648 -4.5910 0. -4.8986e-05 *** x.1.somewhatl -0.6310 0.0424 -14.8846 0. -1.9543e-05 *** x.1.veryl -0.9132 0.0462 -19.7525 0. -4.4603e-05 *** ... [[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-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] grep
Now I've found another way to make it work. All I need is to pick up the names in the column (x.1.age...). > v<-pr(goprobit.p); v Maximum-Likelihood Estimates weighted = FALSE iterations = 5 logLik = -14160.75 finalHessian = TRUE Covariance matrix is Robust Number of parameters = 66 Sample size = 17922 est se t p g sig x.1.age 0.0341 0.0138 2.4766 0.0133 -3.8835e-04 ** x.1.sleep -0.1108 0.0059 -18.6277 0. -4.4572e-04 *** x.1.primary -0.0694 0.0289 -2.4002 0.0164 -9.9638e-06 ** x.1.middle -0.2909 0.0356 -8.1657 0. -1.4913e-05 *** ... > rownames(v) [1] "x.1.age" "x.1.sleep" "x.1.primary" "x.1.middle" [5] "x.1.high" "x.1.somewhath" "x.1.veryh" "x.1.somewhatm" ...Treating it as the first column does not work, because the first column contains the numbers. > v[,1] [1] 0.0341 -0.1108 -0.0694 -0.2909 -0.4267 -0.6188 -0.7580 -0.3413 -0.3813 [10] -0.3101 -0.2977 -0.6310 -0.9132 0.1885 -0.0887 -0.0850 0.0847 -0.1588 On 7/12/2024 11:57 PM, Steven Yen wrote: > Thanks. First and second of the following worked, but the third (with > coef) did not. This may be because I programmed my own estimation > program. In short, > > names(goprobit.p$est) was recognized, but > names(coef(goprobit)) was not > > > which(grepl("very|somewhat",names(goprobit.p$est))) > [1] 6 7 8 9 10 11 12 13 28 29 30 31 32 33 34 35 50 51 52 53 54 55 > 56 57 > > jj<-grep("very|somewhat",names(goprobit.p$est)); length(jj) > [1] 24 > > jj<-grep("very|somewhat",names(coef(goprobit))); length(jj) > Error: object 'goprobit' not found > > On 7/12/2024 11:23 PM, Rui Barradas wrote: >> >> Hello,l >> >> Though the question is already answered, here is another answer to >> what is 'x'. >> The output in the OP is not a lm or glm output but if your regression >> model was programmed according to recommended practices, there must >> be a 'coefficients' member in the list or object it returns and the >> following should work. >> >> >> # this is 'x', a named character vector >> coef(fit) >> # >> fit |> coef() |> names() |> grep("somewhat|very", x = _) >> >> >> Hope this helps, >> >> Rui Barradas >> >> Às 10:26 de 12/07/2024, Steven Yen escreveu: >>> Thanks. In this case below, what is "x"? I tried rownames(out) which >>> did not work. >>> >>> Sorry. Does this sound like homework to you? >>> >>> On 7/12/2024 5:09 PM, Uwe Ligges wrote: >>>> >>>> >>>> On 12.07.2024 10:54, Steven Yen wrote: >>>>> Below is part a regression printout. How can I use "grep" to identify >>>>> rows headed by variables (first column) with a certain label. In this >>>>> case, I like to find variables containing "somewhath", >>>>> "veryh", "somewhatm", "verym", "somewhatc", "veryc","somewhatl", >>>>> "veryl". The result should be an index 6:13 or >>>>> 6,7,8,9,10,11,12,13. Note >>>>> that they all contain "somewhat" and "very". Thanks. >>>> >>>> Sounds like homework? >>>> >>>> which(grep("very|somewhat", x)) >>>> >>>> Best, >>>> Uwe Ligges >>>> >>>> >>>>> est se t p g sig x.1.age 0.0341 0.0138 2.4766 0.0133 -3.8835e-04 ** >>>>> x.1.sleep -0.1108 0.0059 -18.6277 0. -4.4572e-04 *** x.1.primary >>>>> -0.0694 0.0289 -2.4002 0.0164 -9.9638e-06 ** x.1.middle -0.2909 >>>>> 0.0356 >>>>> -8.1657 0. -1.4913e-05 *** x.1.high -0.4267 0.0463 -9.2118 0. >>>>> -3.6246e-05 *** x.1.somewhath -0.6188 0.0256 -24.1971 0. >>>>> -3.1337e-05 >>>>> *** x.1.veryh -0.7580 0.0331 -22.8695 0. -2.9558e-05 *** >>>>> x.1.somewhatm -0.3413 0.0426 -8.0112 0. -1.8920e-05 *** x.1.verym >>>>> -0.3813 0.0446 -8.5413 0. -4.4029e-05 *** x.1.somewhatc -0.3101 >>>>> 0.0649 -4.7783 0. -1.4353e-05 *** x.1.veryc -0.2977 0.0648 >>>>> -4.5910 >>>>> 0. -4.8986e-05 *** x.1.somewhatl -0.6310 0.0424 -14.8846 0. >>>>> -1.9543e-05 *** x.1.veryl -0.9132 0.0462 -19.7525 0. >>>>> -4.4603e-05 *** >>>>> ... >>>>> >>>>> [[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. >> >> [[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] grep
Sorry. grepl worked: which(grepl("very|somewhat",names(goprobit.p$est))) On 7/12/2024 5:34 PM, Steven Yen wrote: > > Could not get "which" to work, but my grep worked. Thanks. > > > which(grep("very|somewhat",names(goprobit.p$est))) Error in > which(grep("very|somewhat", names(goprobit.p$est))) : argument to > 'which' is not logical > grep("very|somewhat",names(goprobit.p$est)) > [1] 6 7 8 9 10 11 12 13 28 29 30 31 32 33 34 35 50 51 52 53 54 55 56 57 > On 7/12/2024 5:26 PM, Steven Yen wrote: >> Thanks. In this case below, what is "x"? I tried rownames(out) which >> did not work. >> >> Sorry. Does this sound like homework to you? >> >> On 7/12/2024 5:09 PM, Uwe Ligges wrote: >>> >>> >>> On 12.07.2024 10:54, Steven Yen wrote: >>>> Below is part a regression printout. How can I use "grep" to identify >>>> rows headed by variables (first column) with a certain label. In this >>>> case, I like to find variables containing "somewhath", >>>> "veryh", "somewhatm", "verym", "somewhatc", "veryc","somewhatl", >>>> "veryl". The result should be an index 6:13 or 6,7,8,9,10,11,12,13. >>>> Note >>>> that they all contain "somewhat" and "very". Thanks. >>> >>> Sounds like homework? >>> >>> which(grep("very|somewhat", x)) >>> >>> Best, >>> Uwe Ligges >>> >>> >>>> est se t p g sig x.1.age 0.0341 0.0138 2.4766 0.0133 -3.8835e-04 ** >>>> x.1.sleep -0.1108 0.0059 -18.6277 0. -4.4572e-04 *** x.1.primary >>>> -0.0694 0.0289 -2.4002 0.0164 -9.9638e-06 ** x.1.middle -0.2909 0.0356 >>>> -8.1657 0. -1.4913e-05 *** x.1.high -0.4267 0.0463 -9.2118 0. >>>> -3.6246e-05 *** x.1.somewhath -0.6188 0.0256 -24.1971 0. >>>> -3.1337e-05 >>>> *** x.1.veryh -0.7580 0.0331 -22.8695 0. -2.9558e-05 *** >>>> x.1.somewhatm -0.3413 0.0426 -8.0112 0. -1.8920e-05 *** x.1.verym >>>> -0.3813 0.0446 -8.5413 0. -4.4029e-05 *** x.1.somewhatc -0.3101 >>>> 0.0649 -4.7783 0. -1.4353e-05 *** x.1.veryc -0.2977 0.0648 -4.5910 >>>> 0. -4.8986e-05 *** x.1.somewhatl -0.6310 0.0424 -14.8846 0. >>>> -1.9543e-05 *** x.1.veryl -0.9132 0.0462 -19.7525 0. >>>> -4.4603e-05 *** >>>> ... >>>> >>>> [[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] grep
Could not get "which" to work, but my grep worked. Thanks. > which(grep("very|somewhat",names(goprobit.p$est))) Error in which(grep("very|somewhat", names(goprobit.p$est))) : argument to 'which' is not logical > grep("very|somewhat",names(goprobit.p$est)) [1] 6 7 8 9 10 11 12 13 28 29 30 31 32 33 34 35 50 51 52 53 54 55 56 57 On 7/12/2024 5:26 PM, Steven Yen wrote: > Thanks. In this case below, what is "x"? I tried rownames(out) which > did not work. > > Sorry. Does this sound like homework to you? > > On 7/12/2024 5:09 PM, Uwe Ligges wrote: >> >> >> On 12.07.2024 10:54, Steven Yen wrote: >>> Below is part a regression printout. How can I use "grep" to identify >>> rows headed by variables (first column) with a certain label. In this >>> case, I like to find variables containing "somewhath", >>> "veryh", "somewhatm", "verym", "somewhatc", "veryc","somewhatl", >>> "veryl". The result should be an index 6:13 or 6,7,8,9,10,11,12,13. >>> Note >>> that they all contain "somewhat" and "very". Thanks. >> >> Sounds like homework? >> >> which(grep("very|somewhat", x)) >> >> Best, >> Uwe Ligges >> >> >>> est se t p g sig x.1.age 0.0341 0.0138 2.4766 0.0133 -3.8835e-04 ** >>> x.1.sleep -0.1108 0.0059 -18.6277 0. -4.4572e-04 *** x.1.primary >>> -0.0694 0.0289 -2.4002 0.0164 -9.9638e-06 ** x.1.middle -0.2909 0.0356 >>> -8.1657 0. -1.4913e-05 *** x.1.high -0.4267 0.0463 -9.2118 0. >>> -3.6246e-05 *** x.1.somewhath -0.6188 0.0256 -24.1971 0. >>> -3.1337e-05 >>> *** x.1.veryh -0.7580 0.0331 -22.8695 0. -2.9558e-05 *** >>> x.1.somewhatm -0.3413 0.0426 -8.0112 0. -1.8920e-05 *** x.1.verym >>> -0.3813 0.0446 -8.5413 0. -4.4029e-05 *** x.1.somewhatc -0.3101 >>> 0.0649 -4.7783 0. -1.4353e-05 *** x.1.veryc -0.2977 0.0648 -4.5910 >>> 0. -4.8986e-05 *** x.1.somewhatl -0.6310 0.0424 -14.8846 0. >>> -1.9543e-05 *** x.1.veryl -0.9132 0.0462 -19.7525 0. -4.4603e-05 >>> *** >>> ... >>> >>> [[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] grep
Thanks. In this case below, what is "x"? I tried rownames(out) which did not work. Sorry. Does this sound like homework to you? On 7/12/2024 5:09 PM, Uwe Ligges wrote: On 12.07.2024 10:54, Steven Yen wrote: Below is part a regression printout. How can I use "grep" to identify rows headed by variables (first column) with a certain label. In this case, I like to find variables containing "somewhath", "veryh", "somewhatm", "verym", "somewhatc", "veryc","somewhatl", "veryl". The result should be an index 6:13 or 6,7,8,9,10,11,12,13. Note that they all contain "somewhat" and "very". Thanks. Sounds like homework? which(grep("very|somewhat", x)) Best, Uwe Ligges est se t p g sig x.1.age 0.0341 0.0138 2.4766 0.0133 -3.8835e-04 ** x.1.sleep -0.1108 0.0059 -18.6277 0. -4.4572e-04 *** x.1.primary -0.0694 0.0289 -2.4002 0.0164 -9.9638e-06 ** x.1.middle -0.2909 0.0356 -8.1657 0. -1.4913e-05 *** x.1.high -0.4267 0.0463 -9.2118 0. -3.6246e-05 *** x.1.somewhath -0.6188 0.0256 -24.1971 0. -3.1337e-05 *** x.1.veryh -0.7580 0.0331 -22.8695 0. -2.9558e-05 *** x.1.somewhatm -0.3413 0.0426 -8.0112 0. -1.8920e-05 *** x.1.verym -0.3813 0.0446 -8.5413 0. -4.4029e-05 *** x.1.somewhatc -0.3101 0.0649 -4.7783 0. -1.4353e-05 *** x.1.veryc -0.2977 0.0648 -4.5910 0. -4.8986e-05 *** x.1.somewhatl -0.6310 0.0424 -14.8846 0. -1.9543e-05 *** x.1.veryl -0.9132 0.0462 -19.7525 0. -4.4603e-05 *** ... [[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] grep
Below is part a regression printout. How can I use "grep" to identify rows headed by variables (first column) with a certain label. In this case, I like to find variables containing "somewhath", "veryh", "somewhatm", "verym", "somewhatc", "veryc","somewhatl", "veryl". The result should be an index 6:13 or 6,7,8,9,10,11,12,13. Note that they all contain "somewhat" and "very". Thanks. est se t p g sig x.1.age 0.0341 0.0138 2.4766 0.0133 -3.8835e-04 ** x.1.sleep -0.1108 0.0059 -18.6277 0. -4.4572e-04 *** x.1.primary -0.0694 0.0289 -2.4002 0.0164 -9.9638e-06 ** x.1.middle -0.2909 0.0356 -8.1657 0. -1.4913e-05 *** x.1.high -0.4267 0.0463 -9.2118 0. -3.6246e-05 *** x.1.somewhath -0.6188 0.0256 -24.1971 0. -3.1337e-05 *** x.1.veryh -0.7580 0.0331 -22.8695 0. -2.9558e-05 *** x.1.somewhatm -0.3413 0.0426 -8.0112 0. -1.8920e-05 *** x.1.verym -0.3813 0.0446 -8.5413 0. -4.4029e-05 *** x.1.somewhatc -0.3101 0.0649 -4.7783 0. -1.4353e-05 *** x.1.veryc -0.2977 0.0648 -4.5910 0. -4.8986e-05 *** x.1.somewhatl -0.6310 0.0424 -14.8846 0. -1.9543e-05 *** x.1.veryl -0.9132 0.0462 -19.7525 0. -4.4603e-05 *** ... [[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] Naming output file
Thanks to all. Editing the line to the following worked: ame<-ame.bopa(get(paste0("bop",im)),y1.level=y1value,y2.level=y2value,jindex=jindex1,vb.method="invH",joint12=TRUE, printing=FALSE,testing=TRUE) On 6/24/2024 9:00 PM, Ivan Krylov wrote: В Mon, 24 Jun 2024 20:16:46 +0800 Steven Yen пишет: In the call to ame.bopa in a loop, I like inputs in the call to ame.bopa to be bop1, bop2, bop3,... Thanks. for (im in 1:m) { ame<-ame.bopa(bop,y1.level=y1value,y2.level=y2value,jindex=jindex1,vb.method="invH",joint12=TRUE, printing=FALSE,testing=TRUE) } Use get(paste0('bop', im)) to read a variable named paste0('bop', im). If you used a list like suggested by Rui, you would be able to use the same syntax for read and write access, namely, bop[[im]], instead of manually assigning variables using assign(name, value) and manually reading variables using get(name). __ 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] Naming output file
Great, thanks. Eric's suggestion is the most simple. Here's a resulting problem. In the call to ame.bopa in a loop, I like inputs in the call to ame.bopa to be bop1, bop2, bop3,... Thanks. for (im in 1:m) { ame<-ame.bopa(bop,y1.level=y1value,y2.level=y2value,jindex=jindex1,vb.method="invH",joint12=TRUE, printing=FALSE,testing=TRUE) } On 6/24/2024 7:57 PM, Eric Berger wrote: > assign(paste0("bop",im),boprobit(eqs,mydata,wt=weight,method="BHHH",tol=0,reltol=0,gradtol=1e-5,Fisher=TRUE)) > > > On Mon, Jun 24, 2024 at 2:56 PM Steven Yen wrote: > > Thanks Eric. I am not following your suggested line. Would you > just edit my line 4? Thanks. > > On 6/24/2024 7:51 PM, Eric Berger wrote: >> How about >> >> assign(paste0("bop",im), boprobit( etc )) >> >> >> >> On Mon, Jun 24, 2024 at 2:41 PM Steven Yen wrote: >> >> I would like a loop to >> >> (1) read data files 2010midata1,2010midata2,2010midata3; and >> >> (2) name OUTPUT bop1,bop2,bop3. >> >> I succeeded in line 3 of the code below, >> >> BUT not line 4. The error message says: >> >> Error in paste0("bop", im) <- boprobit(eqs, mydata, wt = >> weight, method >> = "NR", : target of assignment expands to non-language object >> Please >> help. Thanks. >> >> m<-3 >> for (im in 1:m) { >> mydata<-read.csv(paste0("2010midata",im,".csv")) >> >> paste0("bop",im)<-boprobit(eqs,mydata,wt=weight,method="BHHH",tol=0,reltol=0,gradtol=1e-5,Fisher=TRUE) >> } >> >> >> >> [[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 >> <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] Naming output file
Thanks Eric. I am not following your suggested line. Would you just edit my line 4? Thanks. On 6/24/2024 7:51 PM, Eric Berger wrote: > How about > > assign(paste0("bop",im), boprobit( etc )) > > > > On Mon, Jun 24, 2024 at 2:41 PM Steven Yen wrote: > > I would like a loop to > > (1) read data files 2010midata1,2010midata2,2010midata3; and > > (2) name OUTPUT bop1,bop2,bop3. > > I succeeded in line 3 of the code below, > > BUT not line 4. The error message says: > > Error in paste0("bop", im) <- boprobit(eqs, mydata, wt = weight, > method > = "NR", : target of assignment expands to non-language object Please > help. Thanks. > > m<-3 > for (im in 1:m) { > mydata<-read.csv(paste0("2010midata",im,".csv")) > > paste0("bop",im)<-boprobit(eqs,mydata,wt=weight,method="BHHH",tol=0,reltol=0,gradtol=1e-5,Fisher=TRUE) > } > > > > [[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 > <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] Naming output file
I would like a loop to (1) read data files 2010midata1,2010midata2,2010midata3; and (2) name OUTPUT bop1,bop2,bop3. I succeeded in line 3 of the code below, BUT not line 4. The error message says: Error in paste0("bop", im) <- boprobit(eqs, mydata, wt = weight, method = "NR", : target of assignment expands to non-language object Please help. Thanks. m<-3 for (im in 1:m) { mydata<-read.csv(paste0("2010midata",im,".csv")) paste0("bop",im)<-boprobit(eqs,mydata,wt=weight,method="BHHH",tol=0,reltol=0,gradtol=1e-5,Fisher=TRUE) } [[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] Printout and saved results
I just like the subroutine to spit out results (Mean, Std.dev, etc.) and also be able to access the results for further processing, i.e., v$Mean v$Std.dev On 3/26/2024 11:24 AM, Richard O'Keefe wrote: Not clear what you mean by "saved". If you call a function and the result is printed, the result is remembered for a wee while in the variable .Last.value, so you can do function.with.interesting.result(...) retained.interesting.result <- .Last.value or even .Last.value -> retained.interesting.result If you know before you start writing the expression that you want to save the value, you can wrap the assignment in parentheses, making it an expression: (retained.interesting.result <- function.with.interesting.result(..)) On Tue, 26 Mar 2024 at 15:03, Steven Yen wrote: How can I have both printout and saved results at the same time. The subroutine first return "out" and the printout gets printed, but not saved. I then run the "invisible" line. Results got saved and accessible but no printout. How can I have both printout and also have the results saved? Thank you! > dstat4 <- function(data,digits=3){ + Mean<- apply(data,2,mean,na.rm=TRUE) + Std.dev <- apply(data,2,sd, na.rm=TRUE) + Min <- apply(data,2,min,na.rm=TRUE) + Max <- apply(data,2,max,na.rm=TRUE) + Obs <- dim(data)[1] + out <-round(cbind(Mean,Std.dev,Min,Max,Obs),digits) + out + # invisible(list(Mean=Mean,Std.dev=Std.dev,Min=Min,Max=Max)) + } > x1<-rnorm(n=5,mean=5, sd=1) > x2<-rnorm(n=5,mean=10,sd=2) > w<-rnorm(n=5,mean=2,sd=0.3) > mydata<-data.frame(cbind(x1,x2)) > v<-dstat4(mydata); v Mean Std.dev MinMax Obs x1 5.000 0.922 3.900 6.282 5 x2 10.769 1.713 9.209 13.346 5 > v$Mean Error in v$Mean : $ operator is invalid for atomic vectors > dstat4 <- function(data,digits=3){ + Mean<- apply(data,2,mean,na.rm=TRUE) + Std.dev <- apply(data,2,sd, na.rm=TRUE) + Min <- apply(data,2,min,na.rm=TRUE) + Max <- apply(data,2,max,na.rm=TRUE) + Obs <- dim(data)[1] + out <-round(cbind(Mean,Std.dev,Min,Max,Obs),digits) + # out + invisible(list(Mean=Mean,Std.dev=Std.dev,Min=Min,Max=Max)) + } > v<-dstat4(mydata) > v$Mean x1 x2 4.233051 9.564454 __ 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] Printout and saved results
How can I have both printout and saved results at the same time. The subroutine first return "out" and the printout gets printed, but not saved. I then run the "invisible" line. Results got saved and accessible but no printout. How can I have both printout and also have the results saved? Thank you! > dstat4 <- function(data,digits=3){ + Mean <- apply(data,2,mean,na.rm=TRUE) + Std.dev <- apply(data,2,sd, na.rm=TRUE) + Min <- apply(data,2,min,na.rm=TRUE) + Max <- apply(data,2,max,na.rm=TRUE) + Obs <- dim(data)[1] + out <-round(cbind(Mean,Std.dev,Min,Max,Obs),digits) + out + # invisible(list(Mean=Mean,Std.dev=Std.dev,Min=Min,Max=Max)) + } > x1<-rnorm(n=5,mean=5, sd=1) > x2<-rnorm(n=5,mean=10,sd=2) > w<-rnorm(n=5,mean=2,sd=0.3) > mydata<-data.frame(cbind(x1,x2)) > v<-dstat4(mydata); v Mean Std.dev Min Max Obs x1 5.000 0.922 3.900 6.282 5 x2 10.769 1.713 9.209 13.346 5 > v$Mean Error in v$Mean : $ operator is invalid for atomic vectors > dstat4 <- function(data,digits=3){ + Mean <- apply(data,2,mean,na.rm=TRUE) + Std.dev <- apply(data,2,sd, na.rm=TRUE) + Min <- apply(data,2,min,na.rm=TRUE) + Max <- apply(data,2,max,na.rm=TRUE) + Obs <- dim(data)[1] + out <-round(cbind(Mean,Std.dev,Min,Max,Obs),digits) + # out + invisible(list(Mean=Mean,Std.dev=Std.dev,Min=Min,Max=Max)) + } > v<-dstat4(mydata) > v$Mean x1 x2 4.233051 9.564454 __ 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] Initializing vector and matrices
Thanks to all. Great ideas. I found Eik Vettorazzi's suggesstion easy to implrment: ebarm<-vbarm<-NULL ... if (is.null(ebarm)) ebarm<-ame.00$ei/k else ebarm<-ebarm+ame.00$ei/k if (is.null(vbarm)) vbarm<-ame.00$vi/k else vbarm<-vbarm+ame.00$vi/k ... Steven Yen On 2/29/2024 10:31 PM, Ebert,Timothy Aaron wrote: You could declare a matrix much larger than you intend to use. This works with a few megabytes of data. It is not very efficient, so scaling up may become a problem. m22 <- matrix(NA, 1:60, ncol=6) It does not work to add a new column to the matrix, as in you get an error if you try m22[ , 7] but convert to data frame and add a column m23 <- data.frame(m22) m23$x7 <- 12 The only penalty that I know of to having unused space in a matrix is the amount of memory it takes. One side effect is that your program may have a mistake that you would normally catch with a subscript out of bounds error but with the extra space it now runs without errors. Tim -Original Message- From: R-help On Behalf Of Richard O'Keefe Sent: Thursday, February 29, 2024 5:29 AM To: Steven Yen Cc: R-help Mailing List Subject: Re: [R] Initializing vector and matrices [External Email] x <- numeric(0) for (...) { x[length(x)+1] <- ... } works. You can build a matrix by building a vector one element at a time this way, and then reshaping it at the end. That only works if you don't need it to be a matrix at all times. Another approach is to build a list of rows. It's not a matrix, but a list of rows can be a *ragged* matrix with rows of varying length. On Wed, 28 Feb 2024 at 21:57, Steven Yen wrote: Is there as way to initialize a vector (matrix) with an unknown length (dimension)? NULL does not seem to work. The lines below work with a vector of length 4 and a matrix of 4 x 4. What if I do not know initially the length/dimension of the vector/matrix? All I want is to add up (accumulate) the vector and matrix as I go through the loop. Or, are there other ways to accumulate such vectors and matrices? > x<-rep(0,4) # this works but I like to leave the length open > for (i in 1:3){ + x1<-1:4 + x<-x+x1 + } > x [1] 3 6 9 12 > y = 0*matrix(1:16, nrow = 4, ncol = 4); # this works but I like to leave the dimension open [,1] [,2] [,3] [,4] [1,]0000 [2,]0000 [3,]0000 [4,]0000 > for (i in 1:3){ + y1<-matrix(17:32, nrow = 4, ncol = 4) + y<-y+y1 + } > y [,1] [,2] [,3] [,4] [1,] 51 63 75 87 [2,] 54 66 78 90 [3,] 57 69 81 93 [4,] 60 72 84 96 > __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat/ .ethz.ch%2Fmailman%2Flistinfo%2Fr-help=05%7C02%7Ctebert%40ufl.edu %7Cdbccaccf29674b10b17308dc39114d38%7C0d4da0f84a314d76ace60a62331e1b84 %7C0%7C0%7C638447993707432549%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAw MDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C= PtWjcDOnwO7PArVOSdgYbpz8ksjDPK%2Bn9ySyhwQC0gE%3D=0 PLEASE do read the posting guide http://www.r/ -project.org%2Fposting-guide.html=05%7C02%7Ctebert%40ufl.edu%7Cdb ccaccf29674b10b17308dc39114d38%7C0d4da0f84a314d76ace60a62331e1b84%7C0% 7C0%7C638447993707438911%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiL CJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C=Igb16 CBYgG21HLEDH4I4gfjjFBa3KjDFK8yEZUmBo8s%3D=0 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] [EXT] Initializing vector and matrices
Hello Eik: Thanks. I do not need to sample. Essentially, I have a do loop which produces 24 vectors of length of some length (say k=300) and 24 matrices of 300x300. Then, I simply need to take the averages of these 24 vectors and matrices: x=(x1+x2+...+x24)/k y=(y1+y2+...+y24)/k I am just looking for ways to do this in a do loop, which requires initialization (to 0's) of x and y. My struggle is not knowning length of x until x1 is produced in the first of the loop. Thanks. Steven On 2/28/2024 6:22 PM, Eik Vettorazzi wrote: Hi Steven, It's not entirely clear what you actually want to achieve in the end. As soon as you "know" x1, and assuming that the different "xi" do not differ in length in the real application, you know the length of the target vector. Instead of the loop, you can use 'Reduce' without having to initialize a starting vector. # generate sample vectors, put them in a list xi<-lapply(1:5, \(x)sample(5)) # look at xi xi # sum over xi Reduce("+",xi) this works also for matrices # generate sample matrices, put them in a list Xi<-lapply(1:3, \(x)matrix(sample(16), nrow=4)) # look at them Xi # sum over Xi Reduce("+",Xi) Hope that helps Eik Am 28.02.2024 um 09:56 schrieb Steven Yen: Is there as way to initialize a vector (matrix) with an unknown length (dimension)? NULL does not seem to work. The lines below work with a vector of length 4 and a matrix of 4 x 4. What if I do not know initially the length/dimension of the vector/matrix? All I want is to add up (accumulate) the vector and matrix as I go through the loop. Or, are there other ways to accumulate such vectors and matrices? > x<-rep(0,4) # this works but I like to leave the length open > for (i in 1:3){ + x1<-1:4 + x<-x+x1 + } > x [1] 3 6 9 12 > y = 0*matrix(1:16, nrow = 4, ncol = 4); # this works but I like to leave the dimension open [,1] [,2] [,3] [,4] [1,] 0 0 0 0 [2,] 0 0 0 0 [3,] 0 0 0 0 [4,] 0 0 0 0 > for (i in 1:3){ + y1<-matrix(17:32, nrow = 4, ncol = 4) + y<-y+y1 + } > y [,1] [,2] [,3] [,4] [1,] 51 63 75 87 [2,] 54 66 78 90 [3,] 57 69 81 93 [4,] 60 72 84 96 > __ 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] Initializing vector and matrices
OK. I initialize real large vector and matrix and then shrink them when I use them in the loop. The following lines worked. I'd glad to know of better approaches. bsum<-rep(0,1000); bsum vsum<-matrix(rep(0,100),nrow=1000); vsum for (ind in 1:3) { mydata <- read.csv(paste0("midata", ind, ".csv")) ... k<-length(ame.00$bame) bsum<-bsum[1:k]+ame.00$bame vsum<-vsum[1:k,1:k]+ame.00$vame } On 2/28/2024 4:56 PM, Steven Yen wrote: Is there as way to initialize a vector (matrix) with an unknown length (dimension)? NULL does not seem to work. The lines below work with a vector of length 4 and a matrix of 4 x 4. What if I do not know initially the length/dimension of the vector/matrix? All I want is to add up (accumulate) the vector and matrix as I go through the loop. Or, are there other ways to accumulate such vectors and matrices? > x<-rep(0,4) # this works but I like to leave the length open > for (i in 1:3){ + x1<-1:4 + x<-x+x1 + } > x [1] 3 6 9 12 > y = 0*matrix(1:16, nrow = 4, ncol = 4); # this works but I like to leave the dimension open [,1] [,2] [,3] [,4] [1,] 0 0 0 0 [2,] 0 0 0 0 [3,] 0 0 0 0 [4,] 0 0 0 0 > for (i in 1:3){ + y1<-matrix(17:32, nrow = 4, ncol = 4) + y<-y+y1 + } > y [,1] [,2] [,3] [,4] [1,] 51 63 75 87 [2,] 54 66 78 90 [3,] 57 69 81 93 [4,] 60 72 84 96 > __ 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] Initializing vector and matrices
Is there as way to initialize a vector (matrix) with an unknown length (dimension)? NULL does not seem to work. The lines below work with a vector of length 4 and a matrix of 4 x 4. What if I do not know initially the length/dimension of the vector/matrix? All I want is to add up (accumulate) the vector and matrix as I go through the loop. Or, are there other ways to accumulate such vectors and matrices? > x<-rep(0,4) # this works but I like to leave the length open > for (i in 1:3){ + x1<-1:4 + x<-x+x1 + } > x [1] 3 6 9 12 > y = 0*matrix(1:16, nrow = 4, ncol = 4); # this works but I like to leave the dimension open [,1] [,2] [,3] [,4] [1,] 0 0 0 0 [2,] 0 0 0 0 [3,] 0 0 0 0 [4,] 0 0 0 0 > for (i in 1:3){ + y1<-matrix(17:32, nrow = 4, ncol = 4) + y<-y+y1 + } > y [,1] [,2] [,3] [,4] [1,] 51 63 75 87 [2,] 54 66 78 90 [3,] 57 69 81 93 [4,] 60 72 84 96 > __ 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] Including an external set of coded
How can I call and include an external set of R codes, not necessarily a complete procedure (which can be include with a “source” command). Example: #I like to include and run the following lines residing in a file outside the main program: mydata<-transform(mydata, a<-b+c d<-e+f } Steven from iPhone [[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] Looping
Steven from iPhone > On Feb 19, 2024, at 4:56 PM, Steven Yen wrote: > > Thanks to all. Glad there are many options. > > Steven from iPhone > >>> On Feb 19, 2024, at 1:55 PM, Rui Barradas wrote: >>> >> Às 03:27 de 19/02/2024, Steven Yen escreveu: >>> I need to read csv files repeatedly, named data1.csv, data2.csv,… >>> data24.csv, 24 altogether. That is, >>> data<-read.csv(“data1.csv”) >>> … >>> data<-read.csv(“data24.csv”) >>> … >>> Is there a way to do this in a loop? Thank you. >>> Steven from iPhone >>>[[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. >> Hello, >> >> Here is a way of reading the files in a *apply loop. The file names are >> created by getting them from file (list.files) or by a string editing >> function (sprintf). >> >> >> # file_names_vec <- list.files(pattern = "data\\d+\\.csv") >> file_names_vec <- sprintf("data%d.csv", 1:24) >> data_list <- sapply(file_names_vec, read.csv, simplify = FALSE) >> >> # access the 1st data.frame >> data_list[[1L]] >> # same as above >> data_list[["data1.csv"]] >> # same as above >> data_list$data1.csv >> >> >> Hope this helps, >> >> Rui Barradas >> >> >> >> -- >> Este e-mail foi analisado pelo software antivírus AVG para verificar a >> presença de vírus. >> www.avg.com [[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] Looping
I need to read csv files repeatedly, named data1.csv, data2.csv,… data24.csv, 24 altogether. That is, data<-read.csv(“data1.csv”) … data<-read.csv(“data24.csv”) … Is there a way to do this in a loop? Thank you. Steven from iPhone [[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] Concordance and Kendall's tau in copula
Dear I estimate a sample selection model using the Clayton copula and Burr and Gaussian marginal. I need to derive ther Kendall'sw tau from the concordance coefficient by integration. I came across a way to do that in R long time ago but cannot find it again. Can somewone tell me what to read and what to use? Thank you. Steven Yen __ 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] Yext in parentheses.
Dear All My program is long and sorry I do not have a replicable set of codes to present. But I present a chunk of codes at the end below. Essentially, 1. I initialize cat.ref as NUL (see line 1) 2. Then, I repeatedly add elements to cat.ref, where each element include parentheses in double quotations (see line 2). I had expected cat.ref to eventually contain the list dilemma1(ref),scigrn1(ref),... Not so, I end up getting the following (see first column; not in parentheses, like (ref.)). dilemma1.ref.. 22.356 2.619 8.535 0.000 *** scigrn1.ref.. 22.474 2.697 8.334 0.000 *** Any idea how I might revise lines like the following (first line below): dv.group<-c("dilemma2","dilemma3"); cat.ref<-"dilemma1(ref.)" etc. Thanks. ap0<-zx.ref<-NULL dv.group<-c("dilemma2","dilemma3"); cat.ref<-"dilemma1(ref.)" if(any(dv.group%in%jindex)){ v<-pred0(dv.group,cat.ref) ap0<-rbind(ap0,v$ap0); zx.ref<-c(zx.ref,v$cat.ref) } dv.group<-c("scigrn2","scigrn3"); cat.ref<-"scigrn1(ref.)" if(any(dv.group%in%jindex)){ v<-pred0(dv.group,cat.ref) ap0<-rbind(ap0,v$ap0); zx.ref<-c(zx.ref,v$cat.ref) } [[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] Stacking matrix columns
I wish to stack columns of a matrix into one column. The following matrix command does it. Any other ways? Thanks. > x<-matrix(1:20,5,4) > x [,1] [,2] [,3] [,4] [1,] 1 6 11 16 [2,] 2 7 12 17 [3,] 3 8 13 18 [4,] 4 9 14 19 [5,] 5 10 15 20 > matrix(x,ncol=1) [,1] [1,] 1 [2,] 2 [3,] 3 [4,] 4 [5,] 5 [6,] 6 [7,] 7 [8,] 8 [9,] 9 [10,] 10 [11,] 11 [12,] 12 [13,] 13 [14,] 14 [15,] 15 [16,] 16 [17,] 17 [18,] 18 [19,] 19 [20,] 20 > __ 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] Stacking matrix columns
I wish to stack columns of a matrix into one column. The following matrix command does it. Any other ways? Thanks. > x<-matrix(1:20,5,4) > x [,1] [,2] [,3] [,4] [1,] 1 6 11 16 [2,] 2 7 12 17 [3,] 3 8 13 18 [4,] 4 9 14 19 [5,] 5 10 15 20 > matrix(x,ncol=1) [,1] [1,] 1 [2,] 2 [3,] 3 [4,] 4 [5,] 5 [6,] 6 [7,] 7 [8,] 8 [9,] 9 [10,] 10 [11,] 11 [12,] 12 [13,] 13 [14,] 14 [15,] 15 [16,] 16 [17,] 17 [18,] 18 [19,] 19 [20,] 20 > __ 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] knitr and tinytex
Here’s what I found: Running the line commands install.packages("knitr") install.packages('tinytex') and interactively in RStudio: Tools->Install Packages -> … both work. Thank you! Steven from iPhone > On Apr 27, 2023, at 8:26 AM, Steven T. Yen wrote: > > Dear tinytex users > > I install knitr and tinytex with the following line commands in RStudio and > it seems to work: > > install.packages("knitr") > install.packages('tinytex') > > In the long past I was told to also run the following lines but now they do > not seem to be needed. > #update.packages(ask = FALSE, checkBuilt = TRUE) > #tinytex::tlmgr_update() > #tinytex::reinstall_tinytex() > > Would running the first two lines above be adequate? Is it also OK run run in > RStudio by Tool -> Install.packages? Thanks you! > > [[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] R does not run under latest RStudio
The RStudio list generally does not respond to free version users. I was hoping someone one this (R) list would be kind enough to help me. Steven from iPhone > On Apr 6, 2023, at 6:22 PM, Uwe Ligges > wrote: > > No, but you need to ask on an RStudio mailing list. > This one is about R. > > Best, > Uwe Ligges > > > > >> On 06.04.2023 11:28, Steven T. Yen wrote: >> I updated to latest RStudio (RStudio-2023.03.0-386.exe) but >> R would not run. Error message: >> Error Starting R >> The R session failed to start. >> RSTUDIO VERSION >> RStudio 2023.03.0+386 "Cherry Blossom " (3c53477a, 2023-03-09) for Windows >> [No error available] >> I also tried RStudio 2022.12.0+353 --- same problem. >> I then tried another older version of RStudio (not sure version >> as I changed file name by accident) and R ran. >> Any clues? Please help. 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.
Re: [R] Removing variables from data frame with a wile card
Great, Thanks. Now I have many options. Steven from iPhone > On Feb 13, 2023, at 10:52 AM, Andrew Simmons wrote: > > What I meant is that that > > mydata[, !grepl("^yr", colnames(mydata)), drop = FALSE] > > and > > mydata[!grepl("^yr", colnames(mydata))] > > should be identical. Some people would prefer the first because the > indexing looks the same as matrix indexing, whereas some people would > prefer the second because it is more efficient. However, I would argue > it is exactly as efficient. You can see from the first few lines of > `[.data.frame` when the first index is missing and the second is > provided, it does almost the same thing as if only the first index > provided. > >> On Sun, Feb 12, 2023 at 9:38 PM Steven Yen wrote: >> >> x[“V2”] would retain columns of x headed by V2. What I need is the >> opposite——I need a data grime with those columns excluded. >> >> Steven from iPhone >> >> On Feb 13, 2023, at 9:33 AM, Rolf Turner wrote: >> >> >> On Sun, 12 Feb 2023 14:57:36 -0800 >> Jeff Newmiller wrote: >> >> x["V2"] >> >> >> is more efficient than using drop=FALSE, and perfectly normal syntax >> >> (data frames are lists of columns). >> >> >> >> [[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] Removing variables from data frame with a wile card
x[“V2”] would retain columns of x headed by V2. What I need is the opposite——I need a data grime with those columns excluded. Steven from iPhone > On Feb 13, 2023, at 9:33 AM, Rolf Turner wrote: > > >> On Sun, 12 Feb 2023 14:57:36 -0800 >> Jeff Newmiller wrote: >> >> x["V2"] >> >> is more efficient than using drop=FALSE, and perfectly normal syntax >> (data frames are lists of columns). > > > > I never cease to be amazed by the sagacity and perspicacity of the > designers of R. I would have worried that x["V2"] would turn out to be > a *list* (of length 1), but no, it retains the data.frame class, which > is clearly the Right Thing To Do. > > cheers, > > Rolf > > -- > Honorary Research Fellow > Department of Statistics > University of Auckland > Stats. Dep't. phone: +64-9-373-7599 ext. 89622 > Home phone: +64-9-480-4619 > [[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] Removing variables from data frame with a wile card
Thanks to all. Very helpful. Steven from iPhone > On Jan 14, 2023, at 3:08 PM, Andrew Simmons wrote: > > You'll want to use grep() or grepl(). By default, grep() uses extended > regular expressions to find matches, but you can also use perl regular > expressions and globbing (after converting to a regular expression). > For example: > > grepl("^yr", colnames(mydata)) > > will tell you which 'colnames' start with "yr". If you'd rather you > use globbing: > > grepl(glob2rx("yr*"), colnames(mydata)) > > Then you might write something like this to remove the columns starting with > yr: > > mydata <- mydata[, !grepl("^yr", colnames(mydata)), drop = FALSE] > >> On Sat, Jan 14, 2023 at 1:56 AM Steven T. Yen wrote: >> >> I have a data frame containing variables "yr3",...,"yr28". >> >> How do I remove them with a wild cardsomething similar to "del yr*" >> in Windows/doc? Thank you. >> >>> colnames(mydata) >> [1] "year" "weight" "confeduc" "confothr" "college" >> [6] ... >> [41] "yr3""yr4""yr5""yr6" "yr7" >> [46] "yr8""yr9""yr10" "yr11" "yr12" >> [51] "yr13" "yr14" "yr15" "yr16" "yr17" >> [56] "yr18" "yr19" "yr20" "yr21" "yr22" >> [61] "yr23" "yr24" "yr25" "yr26" "yr27" >> [66] "yr28"... >> >> __ >> 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] cat in a subroutine
No. I just run the subroutine containing those line, with result going to destination go probit1.r.me.it and printed it. Steven from iPhone > On Oct 13, 2022, at 5:09 PM, Jim Lemon wrote: > > Have you assigned the valuie of goprobit1.r.me.kr to "fortytwo", or > indeed anything? > > Jim > >> On Thu, Oct 13, 2022 at 8:02 PM Steven T. Yen wrote: >> >> Not really. >> >> fortytwo<-42 >> cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit >> Probabilities", >> "logisitic =",fortytwo,"\n") >> >>> goprobit1.r.me.kr<-me.gologit.r(goprobit1,embellished=TRUE, >> + resampling=TRUE,ndraws=5); >> goprobit1.r.me.kr >> Error in cat("\nMarginal and Discrete Effects of Gen Ordered Logit / >> Probit Probabilities", : >> unused arguments (fortytwo, "\n") >> >>> On 10/13/2022 4:46 PM, Jim Lemon wrote: >>> Hi Steven & Erin, >>> This works: >>> >>> fortytwo<-42 >>> cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit >>> Probabilities","logisitic =",fortytwo,"\n") >>> j<-grep(".one\\b",c(".one\\a",".one\\b")) >>> >>> Marginal and Discrete Effects of Gen Ordered Logit / Probit >>> Probabilities logisitic = 42 >>> >>> If I don't define fortytwo before calling cat, it doesn't. >>> So we know what the answer is. >>> >>> Jim >>> >>> On Thu, Oct 13, 2022 at 7:36 PM Steven Yen wrote: >>>> t<-abs(me)/se; p<-2*(1-pt(t,nrow(x))) [[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] cat in a subroutine
t<-abs(me)/se; p<-2*(1-pt(t,nrow(x))) sig<-my.sig.levels(p) out<-data.frame(round(cbind(me,se,t,p),digits)); out<-cbind(out,sig) rownames(out)<-names(me) colnames(out)<-c("est","se","t","p","sig") cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit Probabilities", "\n\nlogistic =",logistic) j<-grep(".one\\b",rownames(out)) out<-out[-j,] return(out) } Steven from iPhone > On Oct 13, 2022, at 3:37 PM, Erin Hodgess wrote: > > t<-abs(me)/se; p<-2*(1-pt(t,nrow(x))) > sig<-my.sig.levels(p) > out<-data.frame(round(cbind(me,se,t,p),digits)); out<-cbind(out,sig) > rownames(out)<-names(me) > colnames(out)<-c("est","se","t","p","sig") > cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit > Probabilities", > "\n\nlogistic =",logistic) > j<-grep(".one\\b",rownames(out)) > out<-out[-j,] > return(out) > } [[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] cat in a subroutine
Yes, these lines are part of a subroutine calling yet more procedures. What frustrated me was, I am not doing anything different from other routines. My cat command in the recent code does not do anything other than printing a line of plain text. I am clueless. Steven from iPhone > On Oct 13, 2022, at 3:54 PM, Erin Hodgess wrote: > > > All right. Are these lines of code part of a larger function, please? Is > that function possibly calling a loop, please? > > Thanks, > Erin > >> On Thu, Oct 13, 2022 at 1:49 AM Steven T. Yen wrote: >> Thanks Erin. >> >> No. Removing the second line (so that cat simply prints something else), >> >> cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit >> Probabilities") >> #"\n\nlogistic =",logistic) >> >> I get yet another nonsense: >> >> Error in cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit >> Probabilities") : >> argument "j" is missing, with no default >> > >> On 10/13/2022 3:37 PM, Erin Hodgess wrote: >>> Hi Steven: >>> >>> Do you have a variable called logistic, please? I think that might be the >>> culprit. >>> >>> Thanks, >>> Erin >>> >>> On Thu, Oct 13, 2022 at 1:35 AM Steven T. Yen wrote: I have had an issue with printing (with cat) in a subroutine for which I do not have a applicable example, but I am still hoping to get some help. In the following, the first block of code works fine. ... t<-abs(me)/se; p<-2*(1-pt(t,nrow(x))) sig<-my.sig.levels(p) out<-data.frame(round(cbind(me,se,t,p),digits)); out<-cbind(out,sig) rownames(out)<-names(me) colnames(out)<-c("est","se","t","p","sig") j<-grep(".one\\b",rownames(out)) out<-out[-j,] return(out) } But as soon as I insert lines to print (cat) soething simple, it spits out message that appears to be nonsence (unrelated). Any idea. Please help. Thanks. t<-abs(me)/se; p<-2*(1-pt(t,nrow(x))) sig<-my.sig.levels(p) out<-data.frame(round(cbind(me,se,t,p),digits)); out<-cbind(out,sig) rownames(out)<-names(me) colnames(out)<-c("est","se","t","p","sig") cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit Probabilities", "\n\nlogistic =",logistic) j<-grep(".one\\b",rownames(out)) out<-out[-j,] return(out) } In this particular case, the error message was as follows: Error in cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit Probabilities", : unused argument (logistic) I have printed this way in numerous routines without problem and do not see why this is happending. __ 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. >>> -- >>> Erin Hodgess, PhD >>> mailto: erinm.hodg...@gmail.com > -- > Erin Hodgess, PhD > mailto: erinm.hodg...@gmail.com [[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] Renaming multiple objects
Thanks to all who responded and helped. It worked great! On 2021/11/20 下午 10:31, Rui Barradas wrote: Hello, You can get all objects to be changed into a list, change the list's names attribute and assign back to the globalenv. Something like the following. First see if the objects exist in the global env. ls() #[1] "meb1.p.emb" "meb2.p.emb" "mec1.p.emb" "mec2.p.emb" #[5] "mej12.p.emb" "mej22.p.emb" Now the code to change their names # create a vector of names of the objects # whose names are to be changed obj_names <- ls(pattern = "\\.emb$") # this is instruction is not strictly needed # it's meant to check if the regex works (it does) sub("\\.emb$", "", obj_names) #[1] "meb1.p" "meb2.p" "mec1.p" "mec2.p" "mej12.p" "mej22.p" # get the objects into a list tmp_list <- mget(obj_names, envir = .GlobalEnv) # change the list's names names(tmp_list) <- sub("\\.emb$", "", obj_names) # assign them to the global environment list2env(tmp_list, envir = .GlobalEnv) # clean up rm(tmp_list) rm(list = obj_names) # check to see if it worked (it did) ls() #[1] "meb1.p" "meb2.p" "mec1.p" "mec2.p" "mej12.p" "mej22.p" Hope this helps, Rui Barradas Às 10:27 de 20/11/21, Steven Yen escreveu: I have named NUMEROUS objects (each containing, e.g., 48 obs. of 5 variables), such as mec1.p.emb mec2.p.emb meb1.p.emb meb2.p.emb mej12.p.emb mej22.p.emb How would I rename these objects removing the silly ".emb", into objects mec1.p mec2.p meb1.p meb2.p mej12.p mej22.p 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] Renaming multiple objects
I have named NUMEROUS objects (each containing, e.g., 48 obs. of 5 variables), such as mec1.p.emb mec2.p.emb meb1.p.emb meb2.p.emb mej12.p.emb mej22.p.emb How would I rename these objects removing the silly ".emb", into objects mec1.p mec2.p meb1.p meb2.p mej12.p mej22.p 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] Wild cards for dataframes
Thanks, it works! What can I read to understand more about this part "\\..*$" of the pattern? And more such as ^ and $ that I know from experience? On 2021/10/22 下午 06:22, Rui Barradas wrote: Hello, Use ls() with argument pattern. It accepts a regex and returns a vector of objects names matching the pattern. rm(list = ls(pattern = "data\\..*$")) Hope this helps, Rui Barradas Às 10:20 de 22/10/21, Steven Yen escreveu: I like to be able to use a command with something similar to a "wild card". Below, lines 4 works to delete all three dataframes, but line 5 does not work. Any elegant way to accomplish this? My list of dataframes can be long and so this would be convenient. data.1<-data.frame(x=1:3,y=4:6,z=7:9) data.2<-data.1 data.3<-data.1 rm(data.1,data.2,data.3) rm(data.*) __ 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] Wild cards for dataframes
I like to be able to use a command with something similar to a "wild card". Below, lines 4 works to delete all three dataframes, but line 5 does not work. Any elegant way to accomplish this? My list of dataframes can be long and so this would be convenient. data.1<-data.frame(x=1:3,y=4:6,z=7:9) data.2<-data.1 data.3<-data.1 rm(data.1,data.2,data.3) rm(data.*) __ 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] [External] Subset command
Great. Thanks! On 2021/10/16 上午 09:38, Richard M. Heiberger wrote: the second command doesn't tell R that the variables are in the data.frame mydata. you will need exclude <- with(mydata, prim==-9 | etc) also you will need logical negation ! not arithmetic negation - -c(TRUE,FALSE) [1] -1 0 as.logical(-c(TRUE,FALSE)) [1] TRUE FALSE !c(TRUE,FALSE) [1] FALSE TRUE On Oct 15, 2021, at 21:23, Steven Yen wrote: The following "subset command works. I was hoping the second would as well but it does not. My definition of exclude is rejected. Help please? Thanks. mydata<-subset(mydata, +prim>-9 & highsch>-9 & tert>-9 & +govt>-9 & nongovt>-9 & +married>-9 & urban>-9& +smhmyes>-9 & smhmno>-9 & smhmnoru>-9 & +workouts>-9 & seconhan>-9 & reliyes>-9) exclude<- prim==-9 | highsch==-9 | tert==-9 | +govt==-9 | nongovt==-9 | +married==-9 | urban==-9| +smhmyes==-9 | smhmno==-9 | smhmnoru==-9 | +workouts==-9 | seconhan==-9 | reliyes==-9 Error: object 'prim' not found mydata<-subset(mydata,-exclude) Error in eval(e, x, parent.frame()) : object 'exclude' not found __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-helpdata=04%7C01%7Crmh%40temple.edu%7Ca1d470378fc24c832f7708d99043b804%7C716e81efb52244738e3110bd02ccf6e5%7C0%7C0%7C637699443365577347%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000sdata=DFLP8ZLggvu1NVs9ufyWUdT5hJNKd0v7UYwcHCXncVk%3Dreserved=0 PLEASE do read the posting guide https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.htmldata=04%7C01%7Crmh%40temple.edu%7Ca1d470378fc24c832f7708d99043b804%7C716e81efb52244738e3110bd02ccf6e5%7C0%7C0%7C637699443365577347%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000sdata=4g2tnLBCnnWezCp%2FZIYFRCxrIKa4VDD46WRQohT5Ftk%3Dreserved=0 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] Subset command
Thanks. YES the second call to subset is there, trying to use my failed definition of "exclude". Read on.. On 2021/10/16 上午 09:35, Jeff Newmiller wrote: I don't see a "second one". Looks like you forgot the subset function call? On October 15, 2021 6:23:56 PM PDT, Steven Yen wrote: The following "subset command works. I was hoping the second would as well but it does not. My definition of exclude is rejected. Help please? Thanks. mydata<-subset(mydata, + prim>-9 & highsch>-9 & tert>-9 & + govt>-9 & nongovt>-9 & + married>-9 & urban>-9 & + smhmyes>-9 & smhmno>-9 & smhmnoru>-9 & + workouts>-9 & seconhan>-9 & reliyes>-9) exclude<- prim==-9 | highsch==-9 | tert==-9 | + govt==-9 | nongovt==-9 | + married==-9 | urban==-9 | + smhmyes==-9 | smhmno==-9 | smhmnoru==-9 | + workouts==-9 | seconhan==-9 | reliyes==-9 Error: object 'prim' not found mydata<-subset(mydata,-exclude) Error in eval(e, x, parent.frame()) : object 'exclude' not found __ 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] Subset command
The following "subset command works. I was hoping the second would as well but it does not. My definition of exclude is rejected. Help please? Thanks. > mydata<-subset(mydata, + prim>-9 & highsch>-9 & tert>-9 & + govt>-9 & nongovt>-9 & + married>-9 & urban>-9 & + smhmyes>-9 & smhmno>-9 & smhmnoru>-9 & + workouts>-9 & seconhan>-9 & reliyes>-9) > exclude<- prim==-9 | highsch==-9 | tert==-9 | + govt==-9 | nongovt==-9 | + married==-9 | urban==-9 | + smhmyes==-9 | smhmno==-9 | smhmnoru==-9 | + workouts==-9 | seconhan==-9 | reliyes==-9 Error: object 'prim' not found > mydata<-subset(mydata,-exclude) Error in eval(e, x, parent.frame()) : object 'exclude' not found > __ 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] Variable labels
Thanks. On 2021/5/16 上午 11:52, Robert Knight wrote: > Hi Steven, > > You make great sense wanting to have labels for your variables. When > in RStudio, the little arrow beside "mydata" in the Environment tab > can be clicked and you see all the variables there. And so you would > like to see a description under the variable names. Here is one way > to accomplish that. The following is not pseudocode, it's the actual > code you should use. > > Step 1, create a function that applies an attribute called > "description" to a variable. > desc <- function(obj) attr(obj, "description") > Step 2, use attribute to apply the description > attr(mydata$invpc, "description") <- "Per capita inventory" > Step 3, Now you can either click the arrow beside "mydata" on the > environment tab and see that written description with the word > "description" in quotes. You can also type > desc(mydata$invpc) > And that will provide you the associated description in text form. > > > Robert D. Knight, MBA > > Developer of Meal Plan and Grocery List maker for Android and iOS. > https://play.google.com/store/apps/details?id=io.robertknight.MPGL > <https://play.google.com/store/apps/details?id=io.robertknight.MPGL> > > > > > > > On Wed, May 12, 2021 at 9:49 PM Steven Yen <mailto:st...@ntu.edu.tw>> wrote: > > I insert variable with the expss function as shown below. No error > message. My question is, how to save the variable labels in the data > frame so that I can click to read the labels. Thank you. > > mydata<-read_excel("data/Excel/hseinv.xlsx",na=".") > library(expss) > mydata=apply_labels(mydata, > year ="1947-1988", > inv ="real housing inv, millions $", > pop ="population, 1000s", > price ="housing price index; 1982 = 1", > linv ="log(inv)", > lpop ="log(pop)", > lprice ="log(price)", > t ="time trend: t=1,...,42", > invpc ="per capita inv: inv/pop", > linvpc ="log(invpc)", > lprice_1="lprice[_n-1]", > linvpc_1="linvpc[_n-1]", > gprice ="lprice - lprice_1", > ginvpc ="linvpc - linvpc_1") > > __ > R-help@r-project.org <mailto:R-help@r-project.org> mailing list -- > To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > <https://stat.ethz.ch/mailman/listinfo/r-help> > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > <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] Variable labels
Never mind what I said about "Clickable". All I meant was I created an item "definitions" that appears after I load the binary file, and that I can "click" (don's ask me what I mean by "click") the item in RStudio to read its contents -- variable definitions. All I want to know at this pointis, is whether my way of getting the definitions in the environment "clumsy" and whether there are better ways to do it. Yes, you mention "attr..." but that is not as simple as viewing it in RStudio's environment pane. Thank you! On 2021/5/14 下午 06:37, PIKAL Petr wrote: Hm. What do you mean by "clickable". #I can save any objects to a file save(mydata,definitions, file="test.R") rm("mydata", "definitions") #load them back load("test.R") #but it does not make them "clickable". Point and click is something I am familiar with in Excel or similar programs byt not in R. #objects are back in the environment and one can inspect them by regular way (print, str, head, ...) mydata id age yrmarry 1 1 35 4 2 2 31 6 3 3 21 4 4 4 20 3 5 5 19 7 6 6 24 5 definitions var.labels id Individual ID age Age in Years yrmarry Years of marriage If you want definitions to be part of the data file just use attr. attr(mydata, "var.labels") <- definitions$var.labels attributes(mydata) $names [1] "id" "age" "yrmarry" $class [1] "data.frame" $row.names [1] 1 2 3 4 5 6 $var.labels [1] "Individual ID" "Age in Years" "Years of marriage" Cheers Petr -Original Message- From: R-help On Behalf Of Steven Yen Sent: Friday, May 14, 2021 11:20 AM To: Jim Lemon Cc: R-help Mailing List Subject: Re: [R] Variable labels Thanks to all, for bearing with me. Now I realize expss may not be what I need. I have now written a self- runnable, replicable set of codes (listed below). Perhaps that gives an idea of what I need. Question is, whethet this is the right way to do this (to have a clickable object to learn about variable definitions) or whether there are better ways. Thanks! Steven rm(list=ls()) n<-6 mydata<-data.frame(id=1:n, age=floor(rnorm(n,25,10)), yrmarry=floor(rnorm(n,5,2))) var.labels<-c(id = "Individual ID", age = "Age in Years", yrmarry = "Years of marriage") definitions<-as.data.frame(var.labels) # declare definitions as a data frame save.image("c:/temp/a/try1.RData") # save binary .RData file rm(list=ls()) # clean environment load("c:/temp/a/try1.RData") # now load .RData file and definitions are clickable # all I need is for user to be able to click # and read the variable definitions On 2021/5/14 下午 05:15, Jim Lemon wrote: Hi Steven, I just happened to scan Petr's message to you and wondered if you were looking for something related to the "describe" function in the prettyR package (and a few others). For instance, if you do this: library(prettyR) describe(mtcars) you get this: Description of mtcars Numeric mean median var sd valid.n mpg 20.09 19.2036.32 6.03 32 cyl6.19 6.00 3.19 1.79 32 disp 230.72 196.30 15360.80 123.94 32 hp 146.69 123.00 4700.87 68.56 32 drat 3.60 3.70 0.29 0.53 32 wt 3.22 3.33 0.96 0.98 32 qsec 17.85 17.71 3.19 1.79 32 vs 0.44 0.00 0.25 0.50 32 am 0.41 0.00 0.25 0.50 32 gear 3.69 4.00 0.54 0.74 32 carb 2.81 2.00 2.61 1.62 32 However, you can call almost any summary function as an argument to describe. Suppose I wrote a function "fackey" that produced this output on a factor variable "city": fackey(city) label numericcount New York 1030 London 1523 Paris 1622 Rome 2025 So if you ran "describe" on your data frame, you would get a list of summary data frames that could be saved with the data frame in an .Rdata file. Is this what you are looking for? Jim On Fri, May 14, 2021 at 4:59 PM PIKAL Petr wrote: Hallo Steven You probably need to be more specific what is your intention. I still wonder what is the real problem you want to solve. You loaded binary file and it resulted to 2 data frames. So far so good. But now I am lost. You want to merge info from data frame "desc" to data frame "data"? You can use attr. You want to make binary file which behaves like the one you get? Use save/load. You want to do something different? So plea
Re: [R] Variable labels
Thanks to all, for bearing with me. Now I realize expss may not be what I need. I have now written a self-runnable, replicable set of codes (listed below). Perhaps that gives an idea of what I need. Question is, whethet this is the right way to do this (to have a clickable object to learn about variable definitions) or whether there are better ways. Thanks! Steven rm(list=ls()) n<-6 mydata<-data.frame(id=1:n, age=floor(rnorm(n,25,10)), yrmarry=floor(rnorm(n,5,2))) var.labels<-c(id = "Individual ID", age = "Age in Years", yrmarry = "Years of marriage") definitions<-as.data.frame(var.labels) # declare definitions as a data frame save.image("c:/temp/a/try1.RData") # save binary .RData file rm(list=ls()) # clean environment load("c:/temp/a/try1.RData") # now load .RData file and definitions are clickable # all I need is for user to be able to click # and read the variable definitions On 2021/5/14 下午 05:15, Jim Lemon wrote: Hi Steven, I just happened to scan Petr's message to you and wondered if you were looking for something related to the "describe" function in the prettyR package (and a few others). For instance, if you do this: library(prettyR) describe(mtcars) you get this: Description of mtcars Numeric mean median var sd valid.n mpg 20.09 19.2036.32 6.03 32 cyl6.19 6.00 3.19 1.79 32 disp 230.72 196.30 15360.80 123.94 32 hp 146.69 123.00 4700.87 68.56 32 drat 3.60 3.70 0.29 0.53 32 wt 3.22 3.33 0.96 0.98 32 qsec 17.85 17.71 3.19 1.79 32 vs 0.44 0.00 0.25 0.50 32 am 0.41 0.00 0.25 0.50 32 gear 3.69 4.00 0.54 0.74 32 carb 2.81 2.00 2.61 1.62 32 However, you can call almost any summary function as an argument to describe. Suppose I wrote a function "fackey" that produced this output on a factor variable "city": fackey(city) label numericcount New York 1030 London 1523 Paris 1622 Rome 2025 So if you ran "describe" on your data frame, you would get a list of summary data frames that could be saved with the data frame in an .Rdata file. Is this what you are looking for? Jim On Fri, May 14, 2021 at 4:59 PM PIKAL Petr wrote: Hallo Steven You probably need to be more specific what is your intention. I still wonder what is the real problem you want to solve. You loaded binary file and it resulted to 2 data frames. So far so good. But now I am lost. You want to merge info from data frame "desc" to data frame "data"? You can use attr. You want to make binary file which behaves like the one you get? Use save/load. You want to do something different? So please explain what exactly. Cheers Petr -Original Message- From: Steven Yen Sent: Thursday, May 13, 2021 5:53 PM To: PIKAL Petr Subject: Re: [R] Variable labels Petr Those attachments (1.jpg, 2.jpg) I sent earlier were just screen captures (with a third-party program) of what I saw in the Environment pane right after loading the data. Sorry I cannot explain my questions well enough. All I was showing you was, right after loading the binary data file, I saw two data frames---data which contain the data, and desc which contains definitions of all variables (as shown in 2.jpg). This is a data file from the publisher and I wanted to know what it takes to create a binary data files along with definitions of variables, both in the environment. Steven On 2021/5/13 下午 09:51, PIKAL Petr wrote: Hi Steven I probably do not understand your question correctly. In 1 you show two objects "data" 14x42 data frame and "desc" which is 2x14 data frame, both residing in global environment. In 2 you show contents of data frame desc where variable are probably variable names which are also in data object. names(data) and label which is some more elaborate description of the variable. If you want to move this label into your data object you probably could use attr attr(data, "label") <- desc$label If the order of "variable" is same as the order of data columns. I do not understand what do you mean by - how to get that "desc" in there in the environment? It is already part of global environment. You want to create some new environment and move you desc there? Beside, your images are not familiar to me, this is plain R or some kind of special GUI like R studio? Cheers Petr -Original Message- From: Steven Yen Sent: Thursday, May 13, 2021 1:37 PM To: PIKAL Petr Subject: Re: [R] Variable labels Petr Thanks. I am sending this to you privately as
Re: [R] Variable labels
Thanks. What I need “appears” simple. The .RData file is provided by a third party (likely converted from a different data format such as SAS in which variable labels (not value labels) are common). When I load the binary file, in the “environment” I see, as expected, a data frame showing how many observations for how many variables. In addition, there is also an item (in the environment) (say “desc”) containing a list of variable labels (definitions). I simply like to know how to get “desc” in the environment—-it is a convenient way to show definitions of all variables when you send a binary data file to a third party. Thank you. > On May 13, 2021, at 2:57 PM, Fredrik Karlsson wrote: > > > Hi, > > I am sorry but I don't understand your question, Generally, "clicking" is not > something you can assume to be implemented for anything in R. > However, if you read the manual for the package > > https://gdemin.github.io/expss/ > > you get an example at the bottom where an illustration of how the package can > be used to create Excel tables which would then be easy to interact with > through clicking. > Is that what you wanted? > > Fredrik > >> On Thu, May 13, 2021 at 4:49 AM Steven Yen wrote: >> I insert variable with the expss function as shown below. No error >> message. My question is, how to save the variable labels in the data >> frame so that I can click to read the labels. Thank you. >> >> mydata<-read_excel("data/Excel/hseinv.xlsx",na=".") >> library(expss) >> mydata=apply_labels(mydata, >> year ="1947-1988", >> inv="real housing inv, millions $", >> pop="population, 1000s", >> price ="housing price index; 1982 = 1", >> linv ="log(inv)", >> lpop ="log(pop)", >> lprice ="log(price)", >> t ="time trend: t=1,...,42", >> invpc ="per capita inv: inv/pop", >> linvpc ="log(invpc)", >> lprice_1="lprice[_n-1]", >> linvpc_1="linvpc[_n-1]", >> gprice ="lprice - lprice_1", >> ginvpc ="linvpc - linvpc_1") >> >> __ >> 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. > > > -- > "Life is like a trumpet - if you don't put anything into it, you don't get > anything out of it." [[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] Variable labels
I insert variable with the expss function as shown below. No error message. My question is, how to save the variable labels in the data frame so that I can click to read the labels. Thank you. mydata<-read_excel("data/Excel/hseinv.xlsx",na=".") library(expss) mydata=apply_labels(mydata, year ="1947-1988", inv ="real housing inv, millions $", pop ="population, 1000s", price ="housing price index; 1982 = 1", linv ="log(inv)", lpop ="log(pop)", lprice ="log(price)", t ="time trend: t=1,...,42", invpc ="per capita inv: inv/pop", linvpc ="log(invpc)", lprice_1="lprice[_n-1]", linvpc_1="linvpc[_n-1]", gprice ="lprice - lprice_1", ginvpc ="linvpc - linvpc_1") __ 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] grep
Thank to Rui, Jeff, and Bert. They are all very useful. Somewhat related is the following, in which jindex is a numeric or alphanumeric vector in a function that starts with try<-function(, jindex=NA) In the if loop, in the first line I am trying to determine whether the vector jindex is NA; In the second line, I am trying to determine whether elements in vector jindex is are all non-numeric. Not sure how so I tried to judge by the first element of jindex. Any better way? Thannks. if (!is.na(jindex[1])){ # like to improve this line if(!is.numeric(jindex)[1]){ # like to improve this line words <-jindex pattern<-paste(words,collapse="|") jindex <-grep(pattern=pattern,x.label,value=FALSE) } jj<-jindex; x.label<-x.label[jj] } On 2021/5/9 上午 03:02, Rui Barradas wrote: Hello, The pattern can be assembled with paste(., collapse = "|"). With the same vector of names, nms: words <- c("black","conserv") pattern <- paste(words, collapse = "|") grep(pattern = pattern, nms, value = TRUE) #[1] "x1.black" "x1.conserv" "x2.black" "x2.conserv" Hope this helps, Rui Barradas Às 18:20 de 08/05/21, Jeff Newmiller escreveu: Regular expression patterns are not vectorized... only the data to be searched are. Use one of the many websites dedicated to tutoring regular expressions to learn how they work. (Using function names like "names" as data names is bad practice.) nms <- c( "x1.one", "x1.black", "x1.othrrace", "x1.moddkna", "x1.conserv", "x1.nstrprty", "x1.strrep", "x1.sevngprt", "x2.one", "x2.black", "x2.othrrace", "x2.moddkna", "x2.conserv", "x2.nstrprty", "x2.strrep", "x2.sevngprt" ) grep( "black|conserv", nms, value = TRUE ) On May 8, 2021 10:00:12 AM PDT, Steven Yen wrote: Below, the first command simply creates a list of 16 names (labels) which can be ignore. In the 2nd and 3rd commands, I am able to identify names containing "black". In line 4, I am trying to identify names containing "black" or "conserv" but obviously it does not work. Can someone help? Thanks. names<-names(tp.nohs$estimate)[c(1:8,58:65)]; names [1] "x1.one" "x1.black" "x1.othrrace" "x1.moddkna" "x1.conserv" "x1.nstrprty" [7] "x1.strrep" "x1.sevngprt" "x2.one" "x2.black" "x2.othrrace" "x2.moddkna" [13] "x2.conserv" "x2.nstrprty" "x2.strrep" "x2.sevngprt" grep("black",names,value=TRUE) [1] "x1.black" "x2.black" grep("black",names,value=FALSE) [1] 2 10 grep(c("black","conserv"),names,value=TRUE) [1] "x1.black" "x2.black" Warning message: In grep(c("black", "conserv"), names, value = TRUE) : argument 'pattern' has length > 1 and only the first element will be used __ 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] grep
Below, the first command simply creates a list of 16 names (labels) which can be ignore. In the 2nd and 3rd commands, I am able to identify names containing "black". In line 4, I am trying to identify names containing "black" or "conserv" but obviously it does not work. Can someone help? Thanks. > names<-names(tp.nohs$estimate)[c(1:8,58:65)]; names [1] "x1.one" "x1.black" "x1.othrrace" "x1.moddkna" "x1.conserv" "x1.nstrprty" [7] "x1.strrep" "x1.sevngprt" "x2.one" "x2.black" "x2.othrrace" "x2.moddkna" [13] "x2.conserv" "x2.nstrprty" "x2.strrep" "x2.sevngprt" > grep("black",names,value=TRUE) [1] "x1.black" "x2.black" > grep("black",names,value=FALSE) [1] 2 10 > grep(c("black","conserv"),names,value=TRUE) [1] "x1.black" "x2.black" Warning message: In grep(c("black", "conserv"), names, value = TRUE) : argument 'pattern' has length > 1 and only the first element will be used __ 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] Identifying column type
Thanks. Great idea! Sent from my iPhone Beware: My autocorrect is crazy > On Apr 10, 2021, at 1:37 PM, Rui Barradas wrote: > > Hello, > > Maybe something like > > > ok <- sapply(mydata, is.numeric) > mydata <- mydata[ok] > > > to keep the numeric columns only. > > > Hope this helps, > > Rui Barradas > > Às 04:25 de 10/04/21, Steven Yen escreveu: >> I have data of mixed types in a data frame - date and numeric, as shown >> in summary below. How do I identify the column(s) that is/are not >> numeric, in this case, the first. All I want is to identify the >> column(s) and so that I can remove it/them from the data frame Thanks. >>> summary(mydata) >> Date Spot Futures Min. :1997-09-01 00:00:00 Min. : 735.1 Min. : 734.2 >> 1st Qu.:2002-10-16 12:00:00 1st Qu.:1120.7 1st Qu.:1122.6 Median >> :2007-12-01 00:00:00 Median :1301.8 Median :1303.2 Mean :2007-12-01 >> 06:01:27 Mean :1423.1 Mean :1423.6 3rd Qu.:2013-01-16 12:00:00 3rd >> Qu.:1540.0 3rd Qu.:1546.5 Max. :2018-03-01 00:00:00 Max. :2823.8 Max. >> :2825.8 >>[[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] Identifying column type
Thanks much! Yes it does. I will read. On 2021/4/10 上午 11:33, Jeff Newmiller wrote: Does sapply( mydata, inherits, what = "POSIXt" ) give you any ideas? On April 9, 2021 8:25:36 PM PDT, Steven Yen wrote: I have data of mixed types in a data frame - date and numeric, as shown in summary below. How do I identify the column(s) that is/are not numeric, in this case, the first. All I want is to identify the column(s) and so that I can remove it/them from the data frame Thanks. summary(mydata) Date Spot Futures Min. :1997-09-01 00:00:00 Min. : 735.1 Min. : 734.2 1st Qu.:2002-10-16 12:00:00 1st Qu.:1120.7 1st Qu.:1122.6 Median :2007-12-01 00:00:00 Median :1301.8 Median :1303.2 Mean :2007-12-01 06:01:27 Mean :1423.1 Mean :1423.6 3rd Qu.:2013-01-16 12:00:00 3rd Qu.:1540.0 3rd Qu.:1546.5 Max. :2018-03-01 00:00:00 Max. :2823.8 Max. :2825.8 [[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] Identifying column type
I have data of mixed types in a data frame - date and numeric, as shown in summary below. How do I identify the column(s) that is/are not numeric, in this case, the first. All I want is to identify the column(s) and so that I can remove it/them from the data frame Thanks. > summary(mydata) Date Spot Futures Min. :1997-09-01 00:00:00 Min. : 735.1 Min. : 734.2 1st Qu.:2002-10-16 12:00:00 1st Qu.:1120.7 1st Qu.:1122.6 Median :2007-12-01 00:00:00 Median :1301.8 Median :1303.2 Mean :2007-12-01 06:01:27 Mean :1423.1 Mean :1423.6 3rd Qu.:2013-01-16 12:00:00 3rd Qu.:1540.0 3rd Qu.:1546.5 Max. :2018-03-01 00:00:00 Max. :2823.8 Max. :2825.8 [[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] Column-by-column division
Thanks to all. sweep is convenient. On 2021/3/3 下午 07:16, Rui Barradas wrote: Hello, I forgot about sweep: sweep(x, 2, s, '/') sweep(x, 2, 1:4, '/') Hope this helps, Rui Barradas Às 11:12 de 03/03/21, Rui Barradas escreveu: Hello, Maybe define an infix operator? `%!%` <- function(x, y) { stopifnot(ncol(x) == length(y)) t(t(x)/y) } x <- matrix(1:20, ncol = 2) s <- 1:2 x %!% s x %!% 1:4 Hope this helps, Rui Barradas Às 11:00 de 03/03/21, Steven Yen escreveu: I have a 10 x 2 matrix x. Like to divide the first column by s[1] and second column by s[2]. The following lines work but are clumsy. Any idea? Thanks. > x [,1] [,2] [1,] 1 11 [2,] 2 12 [3,] 3 13 [4,] 4 14 [5,] 5 15 [6,] 6 16 [7,] 7 17 [8,] 8 18 [9,] 9 19 [10,] 10 20 > s [1] 1 2 > t(t(x)/s) [,1] [,2] [1,] 1 5.5 [2,] 2 6.0 [3,] 3 6.5 [4,] 4 7.0 [5,] 5 7.5 [6,] 6 8.0 [7,] 7 8.5 [8,] 8 9.0 [9,] 9 9.5 [10,] 10 10.0 __ 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.
[R] Column-by-column division
I have a 10 x 2 matrix x. Like to divide the first column by s[1] and second column by s[2]. The following lines work but are clumsy. Any idea? Thanks. > x [,1] [,2] [1,] 1 11 [2,] 2 12 [3,] 3 13 [4,] 4 14 [5,] 5 15 [6,] 6 16 [7,] 7 17 [8,] 8 18 [9,] 9 19 [10,] 10 20 > s [1] 1 2 > t(t(x)/s) [,1] [,2] [1,] 1 5.5 [2,] 2 6.0 [3,] 3 6.5 [4,] 4 7.0 [5,] 5 7.5 [6,] 6 8.0 [7,] 7 8.5 [8,] 8 9.0 [9,] 9 9.5 [10,] 10 10.0 __ 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] Calling procedures
Dear All Below are calls to functions to calculate bivariate and univariate logistic probabilities.It works for the following sample program (with results p1=p2 and p3=p4), but similar calls in a more elaborated program produced unpredicted results. My question is whether I am doing something bad (which I should avoid) in my calls to mycdf2 and mycdf to obtain p2 and p3, respectively. Thank you. Steven Yen pbivlogis <- function(x,y,rho){ # * # Bivariate logistic CDF # * p<-(1+exp(-x)+exp(-y)+(1-rho)*exp(-x-y))^(-1) return(p) } mycdf <- function(q,logistic=FALSE){ # * # Univariate CDF: normal or logistic # * if(!logistic){ p<-pnorm(q) } else { p<-plogis(q) } return(p) } mycdf2 <- function(x,y,rho,logistic=FALSE){ # * # Calling bivariate CDF: normal or logistic # * if(!logistic){ p<-pbivnorm(x,y,rho,recycle=T) } else { p<-pbivlogis(x,y,rho) } return(p) } set.seed(123) x<-runif(n=5,min=-3,max=3) y<-runif(n=5,min=-2,max=4) rho<-0.5 p1<-pbivlogis(x,y,rho); p1 p2<-mycdf2(x,y,rho,logistic=TRUE); p2 p3<-mycdf(x,logistic=T); p3 p4<-plogis(x); p4 Results > set.seed(123) > x<-runif(n=5,min=-3,max=3) > y<-runif(n=5,min=-2,max=4) > rho<-0.5 > p1<-pbivlogis(x,y,rho); p1 [1] 0.04937376 0.65977865 0.35821101 0.72243120 0.63881214 > p2<-mycdf2(x,y,rho,logistic=TRUE); p2 [1] 0.04937376 0.65977865 0.35821101 0.72243120 0.63881214 > p3<-mycdf(x,logistic=T); p3 [1] 0.2184819 0.8493908 0.3667608 0.9087199 0.9335661 > p4<-plogis(x); p4 [1] 0.2184819 0.8493908 0.3667608 0.9087199 0.9335661 > __ 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] Defining partial list of variables
Thanks Eric. Yes, "unlist" makes a difference. Below, I am doing not regression but summary to keep the example simple. > set.seed(123) > data<-matrix(runif(1:25),nrow=5) > colnames(data)<-c("x1","x2","x3","x4","x5"); data x1 x2 x3 x4 x5 [1,] 0.2875775 0.0455565 0.9568333 0.89982497 0.8895393 [2,] 0.7883051 0.5281055 0.4533342 0.24608773 0.6928034 [3,] 0.4089769 0.8924190 0.6775706 0.04205953 0.6405068 [4,] 0.8830174 0.5514350 0.5726334 0.32792072 0.9942698 [5,] 0.9404673 0.4566147 0.1029247 0.95450365 0.6557058 > j<-strsplit(gsub("[\n ]","","x1,x3,x5"),",") > j<-unlist(j); j [1] "x1" "x3" "x5" > summary(data[,j]) x1 x3 x5 Min. :0.2876 Min. :0.1029 Min. :0.6405 1st Qu.:0.4090 1st Qu.:0.4533 1st Qu.:0.6557 Median :0.7883 Median :0.5726 Median :0.6928 Mean :0.6617 Mean :0.5527 Mean :0.7746 3rd Qu.:0.8830 3rd Qu.:0.6776 3rd Qu.:0.8895 Max. :0.9405 Max. :0.9568 Max. :0.9943 On 2021/1/5 下午 07:08, Eric Berger wrote: > wrap it in unlist > > xx <- unlist(strsplit( )) > > > > On Tue, Jan 5, 2021 at 12:59 PM Steven Yen <mailto:st...@ntu.edu.tw>> wrote: > > Thanks Eric. Perhaps I should know when to stop. The approach > produces a slightly different variable list (note the [[1]]). > Consequently, I was not able to use xx in defining my regression > formula. > > > x<-colnames(subset(mydata,select=c( > > + hhsize,urban,male, > + age3045,age4659,age60, # age1529 > + highsc,tert, # primary > + gov,nongov, # unemp > + married))); x > [1] "hhsize" "urban" "male" "age3045" "age4659" "age60" > "highsc" "tert" > [9] "gov" "nongov" "married" > > xx<-strsplit(gsub("[\n ]","", > + "hhsize,urban,male, > + age3045,age4659,age60, > + highsc,tert, > + gov,nongov, > + married" > + ),","); xx > [[1]] > [1] "hhsize" "urban" "male" "age3045" "age4659" "age60" > "highsc" "tert" > [9] "gov" "nongov" "married" > > > eq1<-my.formula(y="cig",x=x); eq1 > cig ~ hhsize + urban + male + age3045 + age4659 + age60 + highsc + > tert + gov + nongov + married > > eq2<-my.formula(y="cig",x=xx); eq2 > cig ~ c("hhsize", "urban", "male", "age3045", "age4659", "age60", > "highsc", "tert", "gov", "nongov", "married") > > On 2021/1/5 下午 06:01, Eric Berger wrote: >> If your column names have no spaces the following should work >> >> x<-strsplit(gsub("[\n ]","", >> "hhsize,urban,male, >> + gov,nongov,married"),","); x >> >> On Tue, Jan 5, 2021 at 11:47 AM Steven Yen > <mailto:st...@ntu.edu.tw>> wrote: >> >> Here we go! BUT, it works great for a continuous line. With >> line break(s), I got the nuisance "\n" inserted. >> >> > x<-strsplit("hhsize,urban,male,gov,nongov,married",","); x >> [[1]] >> [1] "hhsize" "urban" "male" "gov" "nongov" "married" >> >> > x<-strsplit("hhsize,urban,male, >> + gov,nongov,married",","); x >> [[1]] >> [1] "hhsize" "urban" "male" >> "\n gov" >> [5] "nongov" "married" >> >> On 2021/1/5 下午 05:34, Eric Berger wrote: >>> >>> zx<-strsplit("age,exercise,income,white,black,hispanic,base,somcol,grad,employed,unable,homeowner,married,divorced,widowed",",") >>> >>> >>> >>> On Tue, Jan 5, 2021 at 11:01 AM Steven Yen >> <mailto:st...@ntu.edu.tw>> wrote: >>> >>> Thank you, Jeff. IMO, we are all here to make R work >>> better to suit our >>>
Re: [R] Defining partial list of variables
Thanks Eric. Perhaps I should know when to stop. The approach produces a slightly different variable list (note the [[1]]). Consequently, I was not able to use xx in defining my regression formula. > x<-colnames(subset(mydata,select=c( + hhsize,urban,male, + age3045,age4659,age60, # age1529 + highsc,tert, # primary + gov,nongov, # unemp + married))); x [1] "hhsize" "urban" "male" "age3045" "age4659" "age60" "highsc" "tert" [9] "gov" "nongov" "married" > xx<-strsplit(gsub("[\n ]","", + "hhsize,urban,male, + age3045,age4659,age60, + highsc,tert, + gov,nongov, + married" + ),","); xx [[1]] [1] "hhsize" "urban" "male" "age3045" "age4659" "age60" "highsc" "tert" [9] "gov" "nongov" "married" > eq1<-my.formula(y="cig",x=x); eq1 cig ~ hhsize + urban + male + age3045 + age4659 + age60 + highsc + tert + gov + nongov + married > eq2<-my.formula(y="cig",x=xx); eq2 cig ~ c("hhsize", "urban", "male", "age3045", "age4659", "age60", "highsc", "tert", "gov", "nongov", "married") On 2021/1/5 下午 06:01, Eric Berger wrote: > If your column names have no spaces the following should work > > x<-strsplit(gsub("[\n ]","", > "hhsize,urban,male, > + gov,nongov,married"),","); x > > On Tue, Jan 5, 2021 at 11:47 AM Steven Yen <mailto:st...@ntu.edu.tw>> wrote: > > Here we go! BUT, it works great for a continuous line. With line > break(s), I got the nuisance "\n" inserted. > > > x<-strsplit("hhsize,urban,male,gov,nongov,married",","); x > [[1]] > [1] "hhsize" "urban" "male" "gov" "nongov" "married" > > > x<-strsplit("hhsize,urban,male, > + gov,nongov,married",","); x > [[1]] > [1] "hhsize" "urban" "male" "\n > gov" > [5] "nongov" "married" > > On 2021/1/5 下午 05:34, Eric Berger wrote: >> >> zx<-strsplit("age,exercise,income,white,black,hispanic,base,somcol,grad,employed,unable,homeowner,married,divorced,widowed",",") >> >> >> >> On Tue, Jan 5, 2021 at 11:01 AM Steven Yen > <mailto:st...@ntu.edu.tw>> wrote: >> >> Thank you, Jeff. IMO, we are all here to make R work better >> to suit our >> various needs. All I am asking is an easier way to define >> variable list >> zx, differently from the way z0 , x0, and treat are defined. >> >> > zx<-colnames(subset(mydata,select=c( >> + >> age,exercise,income,white,black,hispanic,base,somcol,grad,employed, >> + unable,homeowner,married,divorced,widowed))) >> > z0<-c("fruit","highblood") >> > x0<-c("vgood","poor") >> > treat<-"depression" >> > eq1 <-my.formula(y="depression",x=zx,z0) >> > eq2 <-my.formula(y="bmi", x=zx,x0) >> > eq2t<-my.formula(y="bmi", x=zx,treat) >> > eqs<-list(eq1,eq2); eqs >> [[1]] >> depression ~ age + exercise + income + white + black + hispanic + >> base + somcol + grad + employed + unable + homeowner + >> married + >> divorced + widowed + fruit + highblood >> >> [[2]] >> bmi ~ age + exercise + income + white + black + hispanic + base + >> somcol + grad + employed + unable + homeowner + married + >> divorced + widowed + vgood + poor >> >> > eqt<-list(eq1,eq2t); eqt >> [[1]] >> depression ~ age + exercise + income + white + black + hispanic + >> base + somcol + grad + employed + unable + homeowner + >> married + >> divorced + widowed + fruit + highblood >> >> [[2]] >> bmi ~ age + exercise + income + white + black + hispanic + base + >> somcol + grad + employed + unable + homeowne
Re: [R] Defining partial list of variables
Here we go! BUT, it works great for a continuous line. With line break(s), I got the nuisance "\n" inserted. > x<-strsplit("hhsize,urban,male,gov,nongov,married",","); x [[1]] [1] "hhsize" "urban" "male" "gov" "nongov" "married" > x<-strsplit("hhsize,urban,male, + gov,nongov,married",","); x [[1]] [1] "hhsize" "urban" "male" "\n gov" [5] "nongov" "married" On 2021/1/5 下午 05:34, Eric Berger wrote: > zx<-strsplit("age,exercise,income,white,black,hispanic,base,somcol,grad,employed,unable,homeowner,married,divorced,widowed",",") > > > > On Tue, Jan 5, 2021 at 11:01 AM Steven Yen <mailto:st...@ntu.edu.tw>> wrote: > > Thank you, Jeff. IMO, we are all here to make R work better to > suit our > various needs. All I am asking is an easier way to define variable > list > zx, differently from the way z0 , x0, and treat are defined. > > > zx<-colnames(subset(mydata,select=c( > + age,exercise,income,white,black,hispanic,base,somcol,grad,employed, > + unable,homeowner,married,divorced,widowed))) > > z0<-c("fruit","highblood") > > x0<-c("vgood","poor") > > treat<-"depression" > > eq1 <-my.formula(y="depression",x=zx,z0) > > eq2 <-my.formula(y="bmi", x=zx,x0) > > eq2t<-my.formula(y="bmi", x=zx,treat) > > eqs<-list(eq1,eq2); eqs > [[1]] > depression ~ age + exercise + income + white + black + hispanic + > base + somcol + grad + employed + unable + homeowner + married + > divorced + widowed + fruit + highblood > > [[2]] > bmi ~ age + exercise + income + white + black + hispanic + base + > somcol + grad + employed + unable + homeowner + married + > divorced + widowed + vgood + poor > > > eqt<-list(eq1,eq2t); eqt > [[1]] > depression ~ age + exercise + income + white + black + hispanic + > base + somcol + grad + employed + unable + homeowner + married + > divorced + widowed + fruit + highblood > > [[2]] > bmi ~ age + exercise + income + white + black + hispanic + base + > somcol + grad + employed + unable + homeowner + married + > divorced + widowed + depression > > On 2021/1/5 下午 04:18, Jeff Newmiller wrote: > > IMO if you want to hardcode a formula then simply hardcode a > formula. If you want 20 formulas, write 20 formulas. Is that > really so bad? > > > > If you want to have an abbreviated way to specify sets of > variables without conforming to R syntax then put them into data > files and read them in using a format of your choice. > > > > But using NSE to avoid using quotes for entering what amounts to > in-script data is abuse of the language justified by laziness... > the amount of work you put yourself and anyone else who reads your > code through is excessive relative to the benefit gained. > > > > NSE has its strengths... but as a method of creating data > objects it sucks. Note that even the tidyverse (now) requires you > to use quotes when you are not directly referring to something > that already exists. And if you were... you might as well be > creating a formula. > > > > On January 4, 2021 11:14:54 PM PST, Steven Yen <mailto:st...@ntu.edu.tw>> wrote: > >> I constantly define variable lists from a data frame (e.g., to > define a > >> > >> regression equation). Line 3 below does just that. Placing each > >> variable > >> name in quotation marks is too much work especially for a long > list so > >> I > >> do that with line 4. Is there an easier way to accomplish > thisto > >> define a list of variable names containing "a","c","e"? Thank you! > >> > >>> data<-as.data.frame(matrix(1:30,nrow=6)) > >>> colnames(data)<-c("a","b","c","d","e"); data > >> a b c d e > >> 1 1 7 13 19 25 > >> 2 2 8 14 20 26 > >> 3 3 9 15 21 27 > >> 4 4 10 16 22 28 > >> 5 5 11 17 23 29 > >> 6 6 12 18 24 30 > >>> x1<-c("a","c","e
Re: [R] Defining partial list of variables
Thank you, Jeff. IMO, we are all here to make R work better to suit our various needs. All I am asking is an easier way to define variable list zx, differently from the way z0 , x0, and treat are defined. > zx<-colnames(subset(mydata,select=c( + age,exercise,income,white,black,hispanic,base,somcol,grad,employed, + unable,homeowner,married,divorced,widowed))) > z0<-c("fruit","highblood") > x0<-c("vgood","poor") > treat<-"depression" > eq1 <-my.formula(y="depression",x=zx,z0) > eq2 <-my.formula(y="bmi", x=zx,x0) > eq2t<-my.formula(y="bmi", x=zx,treat) > eqs<-list(eq1,eq2); eqs [[1]] depression ~ age + exercise + income + white + black + hispanic + base + somcol + grad + employed + unable + homeowner + married + divorced + widowed + fruit + highblood [[2]] bmi ~ age + exercise + income + white + black + hispanic + base + somcol + grad + employed + unable + homeowner + married + divorced + widowed + vgood + poor > eqt<-list(eq1,eq2t); eqt [[1]] depression ~ age + exercise + income + white + black + hispanic + base + somcol + grad + employed + unable + homeowner + married + divorced + widowed + fruit + highblood [[2]] bmi ~ age + exercise + income + white + black + hispanic + base + somcol + grad + employed + unable + homeowner + married + divorced + widowed + depression On 2021/1/5 下午 04:18, Jeff Newmiller wrote: IMO if you want to hardcode a formula then simply hardcode a formula. If you want 20 formulas, write 20 formulas. Is that really so bad? If you want to have an abbreviated way to specify sets of variables without conforming to R syntax then put them into data files and read them in using a format of your choice. But using NSE to avoid using quotes for entering what amounts to in-script data is abuse of the language justified by laziness... the amount of work you put yourself and anyone else who reads your code through is excessive relative to the benefit gained. NSE has its strengths... but as a method of creating data objects it sucks. Note that even the tidyverse (now) requires you to use quotes when you are not directly referring to something that already exists. And if you were... you might as well be creating a formula. On January 4, 2021 11:14:54 PM PST, Steven Yen wrote: I constantly define variable lists from a data frame (e.g., to define a regression equation). Line 3 below does just that. Placing each variable name in quotation marks is too much work especially for a long list so I do that with line 4. Is there an easier way to accomplish thisto define a list of variable names containing "a","c","e"? Thank you! data<-as.data.frame(matrix(1:30,nrow=6)) colnames(data)<-c("a","b","c","d","e"); data a b c d e 1 1 7 13 19 25 2 2 8 14 20 26 3 3 9 15 21 27 4 4 10 16 22 28 5 5 11 17 23 29 6 6 12 18 24 30 x1<-c("a","c","e"); x1 # line 3 [1] "a" "c" "e" x2<-colnames(subset(data,select=c(a,c,e))); x2 # line 4 [1] "a" "c" "e" __ 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] Defining partial list of variables
I constantly define variable lists from a data frame (e.g., to define a regression equation). Line 3 below does just that. Placing each variable name in quotation marks is too much work especially for a long list so I do that with line 4. Is there an easier way to accomplish thisto define a list of variable names containing "a","c","e"? Thank you! > data<-as.data.frame(matrix(1:30,nrow=6)) > colnames(data)<-c("a","b","c","d","e"); data a b c d e 1 1 7 13 19 25 2 2 8 14 20 26 3 3 9 15 21 27 4 4 10 16 22 28 5 5 11 17 23 29 6 6 12 18 24 30 > x1<-c("a","c","e"); x1 # line 3 [1] "a" "c" "e" > x2<-colnames(subset(data,select=c(a,c,e))); x2 # line 4 [1] "a" "c" "e" __ 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] Printing upon calling a function
Thanks to all. Presenting a large-scale, replicable example can be a burden to the READERs which was why I was reluctant. I am embarrassed to report that after having to restart Windows after the system hang on something unrelated, the issue was resolved and printing was normal. I bet it had nothing to do with the R function. Problem caused by my Windows system memory or something. This sometimes yes, sometimes no situation makes i thard to pinpoint the problem and present a replicable example. I am OK now. Thanks to all. On 2020/11/30 下午 07:21, Duncan Murdoch wrote: By not posting a reproducible example, you're wasting everyone's time. Duncan Murdoch On 30/11/2020 6:06 a.m., Steven Yen wrote: No, sorry. Line 1 below did not print for me and I had to go around and do line 2 to print: me.probit(obj) v<-me.probit(obj); v A puzzle. On 2020/11/30 下午 07:00, Duncan Murdoch wrote: On 30/11/2020 5:41 a.m., Stefan Evert wrote: On 30 Nov 2020, at 10:41, Steven Yen wrote: Thanks. I know, my point was on why I get something printed by simply doing line 1 below and at other occasions had to do line 2. me.probit(obj) That means the return value of me.probit() has been marked as invisible, so it won't auto-print. You have to use an explicit print print(me.probit(obj)) or use your work-around to convince R that you actually meant to print the output. If you dig through the full code of me.probit(), you'll probably find the function invisible() called somewhere. I think you misread his post. "me.probit(obj)" on its own *did* print. It was when he assigned it to a variable using "v <- me.probit(obj)" that it didn't. Assignments are almost always invisible in R. The other thing that people sometimes find confusing is that evaluating expressions that are visible are the top level doesn't make them print when they are nested in a block of code. Usually this happens in a function, e.g. typing a number normally makes it visible, but f <- function() { 1 2 } f() doesn't print 1, it only prints 2, and that happens because 2 is the return value of the function. 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] Printing upon calling a function
No, sorry. Line 1 below did not print for me and I had to go around and do line 2 to print: me.probit(obj) v<-me.probit(obj); v A puzzle. On 2020/11/30 下午 07:00, Duncan Murdoch wrote: On 30/11/2020 5:41 a.m., Stefan Evert wrote: On 30 Nov 2020, at 10:41, Steven Yen wrote: Thanks. I know, my point was on why I get something printed by simply doing line 1 below and at other occasions had to do line 2. me.probit(obj) That means the return value of me.probit() has been marked as invisible, so it won't auto-print. You have to use an explicit print print(me.probit(obj)) or use your work-around to convince R that you actually meant to print the output. If you dig through the full code of me.probit(), you'll probably find the function invisible() called somewhere. I think you misread his post. "me.probit(obj)" on its own *did* print. It was when he assigned it to a variable using "v <- me.probit(obj)" that it didn't. Assignments are almost always invisible in R. The other thing that people sometimes find confusing is that evaluating expressions that are visible are the top level doesn't make them print when they are nested in a block of code. Usually this happens in a function, e.g. typing a number normally makes it visible, but f <- function() { 1 2 } f() doesn't print 1, it only prints 2, and that happens because 2 is the return value of the function. 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] Printing upon calling a function
No. I wrote the function so I am sure no "invisible" command was used. Strangely enough, compiling the function isto part of a package, results were NOT printed. Yes if I call the function during run, by preceding the call with a line that attach the source code: source("A:/.../R/oprobit.R") it did print. I do not understand. On 2020/11/30 下午 06:41, Stefan Evert wrote: On 30 Nov 2020, at 10:41, Steven Yen wrote: Thanks. I know, my point was on why I get something printed by simply doing line 1 below and at other occasions had to do line 2. me.probit(obj) That means the return value of me.probit() has been marked as invisible, so it won't auto-print. You have to use an explicit print print(me.probit(obj)) or use your work-around to convince R that you actually meant to print the output. If you dig through the full code of me.probit(), you'll probably find the function invisible() called somewhere. Best, Stefan __ 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] Printing upon calling a function
Thanks. I know, my point was on why I get something printed by simply doing line 1 below and at other occasions had to do line 2. me.probit(obj) v<-me.probit(obj); v On 2020/11/30 下午 05:33, Jim Lemon wrote: Hi Steven, You seem to be assigning the result of me.oprobit(obj) to v instead of printing it. By appending ";v" tp that command line, you implicitly call "print". Jim On Mon, Nov 30, 2020 at 7:15 PM Steven Yen wrote: I hope I can get away without presenting a replicable set of codes because doing so would impose burdens. I call a function which return a data frame, with the final line return(out) In one case the data frame gets printed (similar to a regression printout), with simply a call me.probit(obj) In another case with a similar function, I could not get the results printed and the only way to print is to do the following: v<-me.oprobit(obj); v This is a puzzle, and I hope to find some clues. Thanks to all. My function looks like the following: me.oprobit0 <- function(obj,mean=FALSE,vb.method,jindex=NA, resampling=FALSE,ndraws=100,mc.method=1,times100=TRUE, Stata.mu=FALSE,testing=FALSE,digits=3){ ... return(out) # out is a data frame } __ 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] Printing upon calling a function
I hope I can get away without presenting a replicable set of codes because doing so would impose burdens. I call a function which return a data frame, with the final line return(out) In one case the data frame gets printed (similar to a regression printout), with simply a call me.probit(obj) In another case with a similar function, I could not get the results printed and the only way to print is to do the following: v<-me.oprobit(obj); v This is a puzzle, and I hope to find some clues. Thanks to all. My function looks like the following: me.oprobit0 <- function(obj,mean=FALSE,vb.method,jindex=NA, resampling=FALSE,ndraws=100,mc.method=1,times100=TRUE, Stata.mu=FALSE,testing=FALSE,digits=3){ ... return(out) # out is a data frame } __ 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] Language environment
Thanks. YES, include the line Sys.setenv(LANG="en"); in my Rprofile file and it worked. On 2020/11/18 上午 12:43, Jeff Newmiller wrote: put it in your .Rprofile file. Read the R Installation and Administration Manusl for more info. On November 17, 2020 5:00:06 AM PST, Steven Yen wrote: In R, I was able to set the language environment by fixing the line in file "C:\Program Files\R\R-4.0.3\etc\Rconsole", line 70 below, set language to EN: language = EN In RStudio, I am not able to do that, except to include the line Sys.setenv(LANG="en"); in every one of my program file. That's too much work. Any idea? Thank you! [[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] Language environment
In R, I was able to set the language environment by fixing the line in file "C:\Program Files\R\R-4.0.3\etc\Rconsole", line 70 below, set language to EN: language = EN In RStudio, I am not able to do that, except to include the line Sys.setenv(LANG="en"); in every one of my program file. That's too much work. Any idea? Thank you! [[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] [External] Re: unable to access index for repository...
Oh Hi Arne, You may recall we visited with this before. I do not believe the problem is algorithm specific. The algorithms I use the most often are BFGS and BHHH (or maxBFGS and maxBHHH). For simple econometric models such as probit, Tobit, and evening sample selection models, old and new versions of R work equally well (I write my own programs and do not use ones from AER or sampleSekection). For more complicated models the newer R would converge with not-so-nice gradients while R-3.0.3 would still do nicely (good gradient). I use numerical graduent of course. I wonder whether numerical gradient routine were revised at the time of transition from R-3.0.3 to newer. Not knowing how different your versions of maxLik are between, I will try as I said I would, that is, use new version of maxLik from old R and vice versa, and see what happens. Sent from my iPhone Beware: My autocorrect is crazy > On Oct 9, 2020, at 4:28 AM, Arne Henningsen wrote: > > Hi Steven > > Which optimisation algorithms in maxLik work better under R-3.0.3 than > under the current version of R? > > /Arne > >> On Thu, 8 Oct 2020 at 21:05, Steven Yen wrote: >> >> Hmm. You raised an interesting point. Actually I am not having problems with >> aod per se—-it is just a supporting package I need while using old R. The >> essential package I need, maxLik, simply works better under R-3.0.3, for >> reason I do not understand—specifically the numerical gradients of the >> likelihood function are not evaluated as accurately in newer versions of R >> in my experience, which is why I continue to use R-3.0.3. Because I use this >> older version of R, naturally I need to install other supporting packages >> such as aod and AER. >> Certainly, I will install the zip file of the older version of maxLik to the >> latest R and see what happens. Thank you. >> >> I will install the new maxLik in old R, and old maxLik in new R, and see >> what happens. >> >> Sent from my iPhone >> Beware: My autocorrect is crazy >> >>>> On Oct 9, 2020, at 2:17 AM, Richard M. Heiberger wrote: >>> >>> I wonder if you are perhaps trying to solve the wrong problem. >>> >>> If you like what the older version of the aod package does, but not >>> the current version, >>> then I think the solution is to propose an option to the aod >>> maintainer that would restore your >>> preferred algorithm into the current version, and then use the current R. >>> >>> A less good, but possibly workable, option is to compile the old >>> version of aod into the current R. __ 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] [External] Re: unable to access index for repository...
Hmm. You raised an interesting point. Actually I am not having problems with aod per se—-it is just a supporting package I need while using old R. The essential package I need, maxLik, simply works better under R-3.0.3, for reason I do not understand—specifically the numerical gradients of the likelihood function are not evaluated as accurately in newer versions of R in my experience, which is why I continue to use R-3.0.3. Because I use this older version of R, naturally I need to install other supporting packages such as aod and AER. Certainly, I will install the zip file of the older version of maxLik to the latest R and see what happens. Thank you. I will install the new maxLik in old R, and old maxLik in new R, and see what happens. Sent from my iPhone Beware: My autocorrect is crazy > On Oct 9, 2020, at 2:17 AM, Richard M. Heiberger wrote: > > I wonder if you are perhaps trying to solve the wrong problem. > > If you like what the older version of the aod package does, but not > the current version, > then I think the solution is to propose an option to the aod > maintainer that would restore your > preferred algorithm into the current version, and then use the current R. > > A less good, but possibly workable, option is to compile the old > version of aod into the current R. > >> On Thu, Oct 8, 2020 at 1:45 PM Jeff Newmiller >> wrote: >> >> All support on this list is voluntary, and support for old versions of R is >> not even necessarily on-topic here which is why you keep getting nudged to >> upgrade. Your "need" for support for an old version is definitely not "our" >> problem, so I suggest you start looking for a consultant if this issue is >> that important to you. Such is the nature of volunteer-developed open source >> software... so support your local experts. >> >>> On October 8, 2020 10:22:54 AM PDT, Steven Yen wrote: >>> Thanks for the help. I have a reason to continue with R-3.0.3. I used >>> maxLik to estimate econometric models and some of them are better >>> handled with R-3.0.3 (but not later)a sad reality I do not like. >>> >>> Here is what I did. I downloaded >>> >>> https://cran-archive.r-project.org/bin/windows/contrib/3.0/aod_1.3.zip >>> >>> and installed the zip file, which worked in both RStudio and R (without >>> >>> RStudio). >>> >>> In RStudio, I go Tools -> Install packages -> Install from -> (Choose >>> zip) -> (Browse to the zip file) >>> >>> IN R, I go Packages -> Install packages from local file(s) -> (Browse >>> to >>> the zip file)... __ 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] unable to access index for repository...
Thanks for the help. I have a reason to continue with R-3.0.3. I used maxLik to estimate econometric models and some of them are better handled with R-3.0.3 (but not later)a sad reality I do not like. Here is what I did. I downloaded https://cran-archive.r-project.org/bin/windows/contrib/3.0/aod_1.3.zip and installed the zip file, which worked in both RStudio and R (without RStudio). In RStudio, I go Tools -> Install packages -> Install from -> (Choose zip) -> (Browse to the zip file) IN R, I go Packages -> Install packages from local file(s) -> (Browse to the zip file)... Below is the command line generated and the prompt. > install.packages("C:/Users/USER/Downloads/aod_1.3.zip", repos = NULL, type = "win.binary") package ‘aod’ successfully unpacked and MD5 sums checked I will always be able to download the proper .zip file from CRAC Archive, right. So, this will always work for me? Thank you all !! If there are more direct options that work, I would still be interested to know. Steven Yen On 2020/10/9 上午 12:49, Duncan Murdoch wrote: Don't choose a mirror. That will override the repos choice. Do update R to a current version if you aren't able to debug this yourself. Duncan Murdoch On 08/10/2020 12:38 p.m., Steven Yen wrote: Sorry Gentlemen and all. Now this is becoming a joke (to me). I repeated what I did earlier, with and without the option to set repos suggested by Duncan. Now it does not work. I wonder whether it is dependent on the mirror I chose, but I do not remember the one I chose earlier when it work. I need your help, gentlemen, as I need to use R-3.0.3 for my task. > options(repos="https://cran-archive.r-project.org;) > chooseCRANmirror() > install.packages("aod") Warning: unable to access index for repository https://cran-archive.r-project.org/bin/windows/contrib/3.0 Warning: unable to access index for repository http://lib.stat.cmu.edu/R/CRAN/bin/windows/contrib/3.0 package ‘aod’ is available as a source package but not as a binary Warning message: package ‘aod’ is not available (for R version 3.0.3) > install.packages("aod",repos='https://cran-archive.r-project.org') Warning: unable to access index for repository https://cran-archive.r-project.org/bin/windows/contrib/3.0 Warning message: package ‘aod’ is not available (for R version 3.0.3) On 2020/10/9 上午 12:02, Duncan Murdoch wrote: Okay, so it's not an RStudio issue. However, I'd guess setting options(repos = "https://cran-archive.r-project.org;) at the start of your session could make everything work. (I'm guessing you currently have it set to "http://cran.rstudio.com;, which is the source of the last warning below, probably due to an R bug. But since you're using an obsolete version of R, it shouldn't surprise you that it has bugs that nobody else is seeing.) Duncan Murdoch On 08/10/2020 11:54 a.m., Steven Yen wrote: Thanks. You gentlemen please tell me what this means. In R (outside of RStudio) I ran: install.packages("aod") Received a warning (and installation did not seem to go through). Then I tried install.packages("aod",repos='https://cran-archive.r-project.org') Received a warning but it went on to try https://cran-archive.r-project.org/bin/windows/contrib/3.0/aod_1.3.zip and it worked. See log below. I expect to continue to use R-3.0.3 for a while and I very much like this to become a routine. Thank you all. Steven Yen > install.packages("aod") Warning in install.packages : unable to access index for repository http://cran.rstudio.com/bin/windows/contrib/3.0 Warning in install.packages : unable to access index for repository http://cran.rstudio.com/bin/windows/contrib/3.0 package ‘aod’ is available as a source package but not as a binary Warning in install.packages : package ‘aod’ is not available (for R version 3.0.3) > install.packages("aod",repos='https://cran-archive.r-project.org') Warning in install.packages : unable to access index for repository http://cran.rstudio.com/bin/windows/contrib/3.0 trying URL 'https://cran-archive.r-project.org/bin/windows/contrib/3.0/aod_1.3.zip' Content type 'application/zip' length 225712 bytes (220 Kb) opened URL downloaded 220 Kb package ‘aod’ successfully unpacked and MD5 sums checked The downloaded binary packages are in C:\Users\USER\AppData\Local\Temp\RtmpY7t6Ll\downloaded_packages On 2020/10/8 下午 10:52, Duncan Murdoch wrote: Just remembered: RStudio runs its own wrapper around install.packages(). Steven, you should try doing the install from outside of RStudio, and see if it makes a difference. Duncan Murdoch On 08/10/2020 9:59 a.m., Duncan Murdoch wrote: He didn't specify the RStudio repos, though it's probably implicitly specified in getOption("repos"). I wonder why install.packages() is looking there, when repos is
Re: [R] unable to access index for repository...
Sorry Gentlemen and all. Now this is becoming a joke (to me). I repeated what I did earlier, with and without the option to set repos suggested by Duncan. Now it does not work. I wonder whether it is dependent on the mirror I chose, but I do not remember the one I chose earlier when it work. I need your help, gentlemen, as I need to use R-3.0.3 for my task. > options(repos="https://cran-archive.r-project.org;) > chooseCRANmirror() > install.packages("aod") Warning: unable to access index for repository https://cran-archive.r-project.org/bin/windows/contrib/3.0 Warning: unable to access index for repository http://lib.stat.cmu.edu/R/CRAN/bin/windows/contrib/3.0 package ‘aod’ is available as a source package but not as a binary Warning message: package ‘aod’ is not available (for R version 3.0.3) > install.packages("aod",repos='https://cran-archive.r-project.org') Warning: unable to access index for repository https://cran-archive.r-project.org/bin/windows/contrib/3.0 Warning message: package ‘aod’ is not available (for R version 3.0.3) On 2020/10/9 上午 12:02, Duncan Murdoch wrote: Okay, so it's not an RStudio issue. However, I'd guess setting options(repos = "https://cran-archive.r-project.org;) at the start of your session could make everything work. (I'm guessing you currently have it set to "http://cran.rstudio.com;, which is the source of the last warning below, probably due to an R bug. But since you're using an obsolete version of R, it shouldn't surprise you that it has bugs that nobody else is seeing.) Duncan Murdoch On 08/10/2020 11:54 a.m., Steven Yen wrote: Thanks. You gentlemen please tell me what this means. In R (outside of RStudio) I ran: install.packages("aod") Received a warning (and installation did not seem to go through). Then I tried install.packages("aod",repos='https://cran-archive.r-project.org') Received a warning but it went on to try https://cran-archive.r-project.org/bin/windows/contrib/3.0/aod_1.3.zip and it worked. See log below. I expect to continue to use R-3.0.3 for a while and I very much like this to become a routine. Thank you all. Steven Yen > install.packages("aod") Warning in install.packages : unable to access index for repository http://cran.rstudio.com/bin/windows/contrib/3.0 Warning in install.packages : unable to access index for repository http://cran.rstudio.com/bin/windows/contrib/3.0 package ‘aod’ is available as a source package but not as a binary Warning in install.packages : package ‘aod’ is not available (for R version 3.0.3) > install.packages("aod",repos='https://cran-archive.r-project.org') Warning in install.packages : unable to access index for repository http://cran.rstudio.com/bin/windows/contrib/3.0 trying URL 'https://cran-archive.r-project.org/bin/windows/contrib/3.0/aod_1.3.zip' Content type 'application/zip' length 225712 bytes (220 Kb) opened URL downloaded 220 Kb package ‘aod’ successfully unpacked and MD5 sums checked The downloaded binary packages are in C:\Users\USER\AppData\Local\Temp\RtmpY7t6Ll\downloaded_packages On 2020/10/8 下午 10:52, Duncan Murdoch wrote: Just remembered: RStudio runs its own wrapper around install.packages(). Steven, you should try doing the install from outside of RStudio, and see if it makes a difference. Duncan Murdoch On 08/10/2020 9:59 a.m., Duncan Murdoch wrote: He didn't specify the RStudio repos, though it's probably implicitly specified in getOption("repos"). I wonder why install.packages() is looking there, when repos is given explicitly? On 08/10/2020 8:54 a.m., Uwe Ligges wrote: Drop the RStudio repos. Best, Uwe Ligges On 05.10.2020 11:10, Steven Yen wrote: Thanks. I did as suggested but still received a warning, though the installation went through. Anything I could do to install without the warning message. What is the contrib.url argument? > install.packages("aod",repos='https://cran-archive.r-project.org') Warning in install.packages : unable to access index for repository http://cran.rstudio.com/bin/windows/contrib/3.0 trying URL 'https://cran-archive.r-project.org/bin/windows/contrib/3.0/aod_1.3.zip' Content type 'application/zip' length 225712 bytes (220 Kb) opened URL downloaded 220 Kb package ‘aod’ successfully unpacked and MD5 sums checked The downloaded binary packages are in C:\Users\USER\AppData\Local\Temp\Rtmp46p9a3\downloaded_packages > On 2020/10/5 下午 04:58, Uwe Ligges wrote: Then you'd rather need install.packages("aod",repos='https://cran-archive.r-project.org') or use the contrib.url argument. Best, Uwe Ligges On 05.10.2020 10:47, Steven Yen wrote: Thanks for the help. I do update to the latest R-4.0.2. As I said, for reasons that's hard to explain, some of my tasks are better handled with an older version of R, in this case R-
Re: [R] unable to access index for repository...
Thanks. You gentlemen please tell me what this means. In R (outside of RStudio) I ran: install.packages("aod") Received a warning (and installation did not seem to go through). Then I tried install.packages("aod",repos='https://cran-archive.r-project.org') Received a warning but it went on to try https://cran-archive.r-project.org/bin/windows/contrib/3.0/aod_1.3.zip and it worked. See log below. I expect to continue to use R-3.0.3 for a while and I very much like this to become a routine. Thank you all. Steven Yen > install.packages("aod") Warning in install.packages : unable to access index for repository http://cran.rstudio.com/bin/windows/contrib/3.0 Warning in install.packages : unable to access index for repository http://cran.rstudio.com/bin/windows/contrib/3.0 package ‘aod’ is available as a source package but not as a binary Warning in install.packages : package ‘aod’ is not available (for R version 3.0.3) > install.packages("aod",repos='https://cran-archive.r-project.org') Warning in install.packages : unable to access index for repository http://cran.rstudio.com/bin/windows/contrib/3.0 trying URL 'https://cran-archive.r-project.org/bin/windows/contrib/3.0/aod_1.3.zip' Content type 'application/zip' length 225712 bytes (220 Kb) opened URL downloaded 220 Kb package ‘aod’ successfully unpacked and MD5 sums checked The downloaded binary packages are in C:\Users\USER\AppData\Local\Temp\RtmpY7t6Ll\downloaded_packages On 2020/10/8 下午 10:52, Duncan Murdoch wrote: > Just remembered: RStudio runs its own wrapper around > install.packages(). Steven, you should try doing the install from > outside of RStudio, and see if it makes a difference. > > Duncan Murdoch > > On 08/10/2020 9:59 a.m., Duncan Murdoch wrote: >> He didn't specify the RStudio repos, though it's probably implicitly >> specified in getOption("repos"). I wonder why install.packages() is >> looking there, when repos is given explicitly? >> >> On 08/10/2020 8:54 a.m., Uwe Ligges wrote: >>> Drop the RStudio repos. >>> >>> Best, >>> Uwe Ligges >>> >>> On 05.10.2020 11:10, Steven Yen wrote: >>>> Thanks. I did as suggested but still received a warning, though the >>>> installation went through. Anything I could do to install without the >>>> warning message. >>>> >>>> What is the contrib.url argument? >>>> >>>> > >>>> install.packages("aod",repos='https://cran-archive.r-project.org') >>>> Warning in install.packages : >>>> unable to access index for repository >>>> http://cran.rstudio.com/bin/windows/contrib/3.0 >>>> trying URL >>>> 'https://cran-archive.r-project.org/bin/windows/contrib/3.0/aod_1.3.zip' >>>> >>>> Content type 'application/zip' length 225712 bytes (220 Kb) >>>> opened URL >>>> downloaded 220 Kb >>>> >>>> package ‘aod’ successfully unpacked and MD5 sums checked >>>> >>>> The downloaded binary packages are in >>>> C:\Users\USER\AppData\Local\Temp\Rtmp46p9a3\downloaded_packages >>>> > >>>> >>>> On 2020/10/5 下午 04:58, Uwe Ligges wrote: >>>>> Then you'd rather need >>>>> >>>>> install.packages("aod",repos='https://cran-archive.r-project.org') >>>>> >>>>> or use the contrib.url argument. >>>>> >>>>> Best, >>>>> Uwe Ligges >>>>> >>>>> >>>>> On 05.10.2020 10:47, Steven Yen wrote: >>>>>> Thanks for the help. I do update to the latest R-4.0.2. As I said, >>>>>> for reasons that's hard to explain, some of my tasks are better >>>>>> handled with an older version of R, in this case R-3.0.3. Please >>>>>> just >>>>>> help me install packages successfully with this older version of R. >>>>>> >>>>>> I ran the following line but obviously was not getting it across. >>>>>> >>>>>> = >>>>>> >>>>>>> install.packages("aod",repos='https://cran-archive.r-project.org/bin/windows/contrib/3.0/') >>>>>>> >>>>>>> >>>>>>> Warning in >>>>>> install.packages : unable to access index for repository >>>>>> http://cran.rstudio.com/bin/windows/contrib/3.0 Warning in >>>>>> install.packages : unable to access index for repository >>&g
Re: [R] unable to access index for repository...
Thanks. I did as suggested but still received a warning, though the installation went through. Anything I could do to install without the warning message. What is the contrib.url argument? > install.packages("aod",repos='https://cran-archive.r-project.org') Warning in install.packages : unable to access index for repository http://cran.rstudio.com/bin/windows/contrib/3.0 trying URL 'https://cran-archive.r-project.org/bin/windows/contrib/3.0/aod_1.3.zip' Content type 'application/zip' length 225712 bytes (220 Kb) opened URL downloaded 220 Kb package ‘aod’ successfully unpacked and MD5 sums checked The downloaded binary packages are in C:\Users\USER\AppData\Local\Temp\Rtmp46p9a3\downloaded_packages > On 2020/10/5 下午 04:58, Uwe Ligges wrote: Then you'd rather need install.packages("aod",repos='https://cran-archive.r-project.org') or use the contrib.url argument. Best, Uwe Ligges On 05.10.2020 10:47, Steven Yen wrote: Thanks for the help. I do update to the latest R-4.0.2. As I said, for reasons that's hard to explain, some of my tasks are better handled with an older version of R, in this case R-3.0.3. Please just help me install packages successfully with this older version of R. I ran the following line but obviously was not getting it across. = install.packages("aod",repos='https://cran-archive.r-project.org/bin/windows/contrib/3.0/') Warning in install.packages : unable to access index for repository http://cran.rstudio.com/bin/windows/contrib/3.0 Warning in install.packages : unable to access index for repository https://cran-archive.r-project.org/bin/windows/contrib/3.0/bin/windows/contrib/3.0 Warning in install.packages : package ‘aod’ is not available (for R version 3.0.3) On 2020/10/5 下午 04:12, Uwe Ligges wrote: From ../ReadMe in the mentioned web resource: "Packages for R >= 1.7.0 and R < 3.2.0 are available from https://cran-archive.r-project.org/bin/windows/contrib/; We do not hold binaries of several year old versions of R on CRAN. Note that R 3.0.0 is 7 years old. You shoudl realy consider to update to 4.0.2. Best, Uwe Ligges On 05.10.2020 09:56, Steven Yen wrote: I had to install/use an older version of (R-3.0.3) for a reason. While installing a package from CRAN (either in RStudio or R), I received the following warning message saying unable to access index for repository http://cran.rstudio.com/bin/windows/contrib/3.0. See message below. In this case, I tried to install "aod". The install nevertheless went through, as confirmed by the library statement (I did not try to use it). Any idea? Thank you. = Warning: unable to access index for repository http://cran.rstudio.com/bin/windows/contrib/3.0 Warning: unable to access index for repository http://cran.rstudio.com/bin/windows/contrib/3.0 > install.packages("aod") Warning in install.packages : unable to access index for repository http://cran.rstudio.com/bin/windows/contrib/3.0 Warning in install.packages : unable to access index for repository http://cran.rstudio.com/bin/windows/contrib/3.0 package ‘aod’ is available as a source package but not as a binary Warning in install.packages : package ‘aod’ is not available (for R version 3.0.3) > library(aod) [[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] unable to access index for repository...
Thanks for the help. I do update to the latest R-4.0.2. As I said, for reasons that's hard to explain, some of my tasks are better handled with an older version of R, in this case R-3.0.3. Please just help me install packages successfully with this older version of R. I ran the following line but obviously was not getting it across. = > install.packages("aod",repos='https://cran-archive.r-project.org/bin/windows/contrib/3.0/') > Warning in install.packages : unable to access index for repository http://cran.rstudio.com/bin/windows/contrib/3.0 Warning in install.packages : unable to access index for repository https://cran-archive.r-project.org/bin/windows/contrib/3.0/bin/windows/contrib/3.0 Warning in install.packages : package ‘aod’ is not available (for R version 3.0.3) On 2020/10/5 下午 04:12, Uwe Ligges wrote: > From > > ../ReadMe > in the mentioned web resource: > > "Packages for R >= 1.7.0 and R < 3.2.0 are available from > https://cran-archive.r-project.org/bin/windows/contrib/; > > We do not hold binaries of several year old versions of R on CRAN. > > Note that R 3.0.0 is 7 years old. You shoudl realy consider to update > to 4.0.2. > > Best, > Uwe Ligges > > > > > On 05.10.2020 09:56, Steven Yen wrote: >> I had to install/use an older version of (R-3.0.3) for a reason. While >> installing a package from CRAN (either in RStudio or R), I received the >> following warning message saying unable to access index for repository >> http://cran.rstudio.com/bin/windows/contrib/3.0. See message below. In >> this case, I tried to install "aod". The install nevertheless went >> through, as confirmed by the library statement (I did not try to use >> it). Any idea? Thank you. >> >> = >> >> Warning: unable to access index for repository >> http://cran.rstudio.com/bin/windows/contrib/3.0 Warning: unable to >> access index for repository >> http://cran.rstudio.com/bin/windows/contrib/3.0 > >> install.packages("aod") Warning in install.packages : unable to access >> index for repository http://cran.rstudio.com/bin/windows/contrib/3.0 >> Warning in install.packages : unable to access index for repository >> http://cran.rstudio.com/bin/windows/contrib/3.0 package ‘aod’ is >> available as a source package but not as a binary Warning in >> install.packages : package ‘aod’ is not available (for R version 3.0.3) >> > library(aod) >> >> >> [[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.
[R] unable to access index for repository...
I had to install/use an older version of (R-3.0.3) for a reason. While installing a package from CRAN (either in RStudio or R), I received the following warning message saying unable to access index for repository http://cran.rstudio.com/bin/windows/contrib/3.0. See message below. In this case, I tried to install "aod". The install nevertheless went through, as confirmed by the library statement (I did not try to use it). Any idea? Thank you. = Warning: unable to access index for repository http://cran.rstudio.com/bin/windows/contrib/3.0 Warning: unable to access index for repository http://cran.rstudio.com/bin/windows/contrib/3.0 > install.packages("aod") Warning in install.packages : unable to access index for repository http://cran.rstudio.com/bin/windows/contrib/3.0 Warning in install.packages : unable to access index for repository http://cran.rstudio.com/bin/windows/contrib/3.0 package ‘aod’ is available as a source package but not as a binary Warning in install.packages : package ‘aod’ is not available (for R version 3.0.3) > library(aod) [[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] Calling a procedure
Thanks to all for educating me about procedures and argument. Those were very helpful!! On 2020/9/21 上午 12:26, Bert Gunter wrote: > Argument passing is fundamental, even more so when you write your own > functions, which any half-serious R user will want to do. What has > heretofore been discussed in this thread is not the whole story (e.g. > there are ... arguments and functions as binary operators, among other > things). See section 10 of the "Introduction to R" document that > ships with R or any other decent R tutorial of your choice. The R > language definition is the definitive reference (section 4 especially > for this). > > All imo of course. > > 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 Sun, Sep 20, 2020 at 8:57 AM Mark Leeds <mailto:marklee...@gmail.com>> wrote: > > Hi Steven: Rui's detailed explanation was great. The way I think > of it is, > if you don't > want to send the variables in with the same order as the formal > arguments, then you > better name them as you send them in. > > > > > > On Sun, Sep 20, 2020 at 7:23 AM Steven Yen <mailto:st...@ntu.edu.tw>> wrote: > > > Thanks. So, to be safe, always a good idea to give the argument, > e.g., > > q=1.96, log.p=FALSE, skipping mean=0 and sd=1 if not needed. Thanks. > > > > pnorm(q=1.96, log.p = FALSE) > > > > On 2020/9/20 下午 06:36, Rui Barradas wrote: > > > Hello, > > > > > > You are making a confusion between > > > > > > 1. the formal argument log.p > > > 2. the variable log.p > > > > > > In the function body, log.p is a variable that exists in the > > > function's frame, not the formal argument of pnorm. > > > The first and the 3rd calls that follow output the same value. > > > > > > try(x = 1.2, log.p = TRUE)$a > > > try(x = 1.2, log.p = TRUE)$b > > > try(x = 1.2, 1)$a > > > > > > This is because in the function > > > > > > a<-pnorm(x,log.p) # first call > > > > > > passes log.p as the *second* argument, not as a value for pnorm's > > > formal argument log.p. Unless when named, the arguments are > passed in > > > the order they appear in the function's definition: > > > > > > pnorm(q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE) > > > > > > and that becomes > > > > > > a<-pnorm(x,TRUE) # first call > > > a<-pnorm(x,1) # first call, coerced to numeric. > > > > > > > > > Let me give another example. In the function that follows the > default > > > is z = FALSE. > > > > > > In the first call the name z is not the name of the argument, > it's the > > > name of a variable that exists in the .GlobalEnv. > > > > > > In the second call, z = z assign the formal argument z the > value of > > > the variable z. > > > > > > > > > f <- function(x, y = 0, z = FALSE){ > > > a <- x > > > b <- y > > > d <- z > > > list(a = a, b = b, d = d) > > > } > > > z <- 2 > > > f(1, z) > > > f(1, z = z) > > > > > > > > > Hope this helps, > > > > > > Rui Barradas > > > > > > Às 11:11 de 20/09/20, Steven Yen escreveu: > > >> Can someone tell me a proper call to a procedure, in this case, > > >> pnorm. In what follows, I had expected a = b, but they are > not equal. > > >> What are wrong with first call and second call? Thank you! > > >> > > >> try<-function(x,log.p=FALSE){ > > >> a<-pnorm(x,log.p) # first call > > >> b<-pnorm(x,log.p=log.p) # second call > > >> list(a=a,b=b) > > >> } > > >> > > >> try(x=1.2,log.p=TRUE)$a > > >> try(x=1.2,log.p=TRUE)$b > > >> > > >> __ > > >> R-help@r-project.org <mailto:R-help@r-project.org&g
Re: [R] Calling a procedure
Thanks. So, to be safe, always a good idea to give the argument, e.g., q=1.96, log.p=FALSE, skipping mean=0 and sd=1 if not needed. Thanks. pnorm(q=1.96, log.p = FALSE) On 2020/9/20 下午 06:36, Rui Barradas wrote: > Hello, > > You are making a confusion between > > 1. the formal argument log.p > 2. the variable log.p > > In the function body, log.p is a variable that exists in the > function's frame, not the formal argument of pnorm. > The first and the 3rd calls that follow output the same value. > > try(x = 1.2, log.p = TRUE)$a > try(x = 1.2, log.p = TRUE)$b > try(x = 1.2, 1)$a > > This is because in the function > > a<-pnorm(x,log.p) # first call > > passes log.p as the *second* argument, not as a value for pnorm's > formal argument log.p. Unless when named, the arguments are passed in > the order they appear in the function's definition: > > pnorm(q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE) > > and that becomes > > a<-pnorm(x,TRUE) # first call > a<-pnorm(x,1) # first call, coerced to numeric. > > > Let me give another example. In the function that follows the default > is z = FALSE. > > In the first call the name z is not the name of the argument, it's the > name of a variable that exists in the .GlobalEnv. > > In the second call, z = z assign the formal argument z the value of > the variable z. > > > f <- function(x, y = 0, z = FALSE){ > a <- x > b <- y > d <- z > list(a = a, b = b, d = d) > } > z <- 2 > f(1, z) > f(1, z = z) > > > Hope this helps, > > Rui Barradas > > Às 11:11 de 20/09/20, Steven Yen escreveu: >> Can someone tell me a proper call to a procedure, in this case, >> pnorm. In what follows, I had expected a = b, but they are not equal. >> What are wrong with first call and second call? Thank you! >> >> try<-function(x,log.p=FALSE){ >> a<-pnorm(x,log.p) # first call >> b<-pnorm(x,log.p=log.p) # second call >> list(a=a,b=b) >> } >> >> try(x=1.2,log.p=TRUE)$a >> try(x=1.2,log.p=TRUE)$b >> >> __ >> 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] Calling a procedure
Can someone tell me a proper call to a procedure, in this case, pnorm. In what follows, I had expected a = b, but they are not equal. What are wrong with first call and second call? Thank you! try<-function(x,log.p=FALSE){ a<-pnorm(x,log.p) # first call b<-pnorm(x,log.p=log.p) # second call list(a=a,b=b) } try(x=1.2,log.p=TRUE)$a try(x=1.2,log.p=TRUE)$b __ 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] Rtools required
Thanks. Can you kindly tell me what to read to do it the "standard way"? Also, where can I find file .Renviron. On 2020/4/28 下午 11:08, Duncan Murdoch wrote: On 28/04/2020 11:02 a.m., Steven Yen wrote: In RStudio, I enter File -> Open Project -> and browse to open a .Rproj file. Then, I click Build -> Build Binary Package. Thanks. Do it the standard way instead of using devtools. Duncan Murdoch On 2020/4/28 下午 10:55, Duncan Murdoch wrote: On 28/04/2020 9:56 a.m., Steven Yen wrote: Thanks. I visited the Rtools web page and learned to run the following lines. I am still getting the same warning message. And you are still not telling us what command you used to trigger that message. Duncan Murdoch > writeLines('PATH="${RTOOLS40_HOME}\\usr\\bin;${PATH}"', con = "~/.Renviron") > Sys.which("make") make "C:\\rtools40\\usr\\bin\\make.exe" On 2020/4/28 下午 08:39, Duncan Murdoch wrote: On 28/04/2020 5:57 a.m., Steven T. Yen wrote: Dear All I updated to R-4.0.0. and also installed the latest Rtools 4.0 (to now the new default folder c:\rtools40). While compiling a package (binary) I received the follow marning message saying Rtools is required. Any clues? Thanks. Presumably you didn't put it on your path, or you used a non-standard way to build. You need to say what command you used. 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] Rtools required
In RStudio, I enter File -> Open Project -> and browse to open a .Rproj file. Then, I click Build -> Build Binary Package. Thanks. On 2020/4/28 下午 10:55, Duncan Murdoch wrote: On 28/04/2020 9:56 a.m., Steven Yen wrote: Thanks. I visited the Rtools web page and learned to run the following lines. I am still getting the same warning message. And you are still not telling us what command you used to trigger that message. Duncan Murdoch > writeLines('PATH="${RTOOLS40_HOME}\\usr\\bin;${PATH}"', con = "~/.Renviron") > Sys.which("make") make "C:\\rtools40\\usr\\bin\\make.exe" On 2020/4/28 下午 08:39, Duncan Murdoch wrote: On 28/04/2020 5:57 a.m., Steven T. Yen wrote: Dear All I updated to R-4.0.0. and also installed the latest Rtools 4.0 (to now the new default folder c:\rtools40). While compiling a package (binary) I received the follow marning message saying Rtools is required. Any clues? Thanks. Presumably you didn't put it on your path, or you used a non-standard way to build. You need to say what command you used. 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] Rtools required
Thanks. Updating RStudio to 1.2.5042 did fix the problem. Thank you! On 2020/4/28 下午 11:30, Duncan Murdoch wrote: On 28/04/2020 11:16 a.m., Steven Yen wrote: Thanks. Can you kindly tell me what to read to do it the "standard way"? Start with ?INSTALL, and find more details in the Writing R Extensions manual. I believe RStudio can be configured to use those tools rather than the devtools ones, but I don't know if it will still run its test for Rtools if you do it that way. I imagine you can also update RStudio and all of your packages; eventually that will work, if this is really the issue. Duncan Murdoch Also, where can I find file .Renviron. On 2020/4/28 下午 11:08, Duncan Murdoch wrote: On 28/04/2020 11:02 a.m., Steven Yen wrote: In RStudio, I enter File -> Open Project -> and browse to open a .Rproj file. Then, I click Build -> Build Binary Package. Thanks. Do it the standard way instead of using devtools. Duncan Murdoch On 2020/4/28 下午 10:55, Duncan Murdoch wrote: On 28/04/2020 9:56 a.m., Steven Yen wrote: Thanks. I visited the Rtools web page and learned to run the following lines. I am still getting the same warning message. And you are still not telling us what command you used to trigger that message. Duncan Murdoch > writeLines('PATH="${RTOOLS40_HOME}\\usr\\bin;${PATH}"', con = "~/.Renviron") > Sys.which("make") make "C:\\rtools40\\usr\\bin\\make.exe" On 2020/4/28 下午 08:39, Duncan Murdoch wrote: On 28/04/2020 5:57 a.m., Steven T. Yen wrote: Dear All I updated to R-4.0.0. and also installed the latest Rtools 4.0 (to now the new default folder c:\rtools40). While compiling a package (binary) I received the follow marning message saying Rtools is required. Any clues? Thanks. Presumably you didn't put it on your path, or you used a non-standard way to build. You need to say what command you used. 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] Rtools required
Thanks. I visited the Rtools web page and learned to run the following lines. I am still getting the same warning message. > writeLines('PATH="${RTOOLS40_HOME}\\usr\\bin;${PATH}"', con = "~/.Renviron") > Sys.which("make") make "C:\\rtools40\\usr\\bin\\make.exe" On 2020/4/28 下午 08:39, Duncan Murdoch wrote: On 28/04/2020 5:57 a.m., Steven T. Yen wrote: Dear All I updated to R-4.0.0. and also installed the latest Rtools 4.0 (to now the new default folder c:\rtools40). While compiling a package (binary) I received the follow marning message saying Rtools is required. Any clues? Thanks. Presumably you didn't put it on your path, or you used a non-standard way to build. You need to say what command you used. 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.
[R] Using older version of R
I had to use an older version of R (as old as R3.0.3) for a reason. I myself have no problem installing a package built under a newer version, but my student (who also installed R3.0.3) could not install the package (newer version). Had an error message saying package x is not available under R3.0.3. Is there a get-arroundto be able to install while running an older R version? (Don't ask me why I run an older R. An essential package I need works well only in R3.0.3). Thank you all. -- st...@ntu.edu.tw (S.T. Yen) [[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] Warning from installing packages in R3.6.1
Hello Duncan: Below I am sending (1) message from installation of a .zip file; (2) from installation of aod from CRAN; (3) from running the line sessionInfo() Looks like both installations were successful despite the warning message. Now the point seems to be to figure out why the warning arises. Thank you. Steven Yen === > install.packages("C:/Users/Bonnie/Desktop/yenlib1_1.1.0.zip", repos = NULL, type = "win.binary") WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding: https://cran.rstudio.com/bin/windows/Rtools/ Installing package into ‘C:/Users/Bonnie/Documents/R/win-library/3.6’ (as ‘lib’ is unspecified) package ‘yenlib1’ successfully unpacked and MD5 sums checked > install.packages("aod") WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding: https://cran.rstudio.com/bin/windows/Rtools/ Installing package into ‘C:/Users/Bonnie/Documents/R/win-library/3.6’ (as ‘lib’ is unspecified) trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/aod_1.3.1.zip' Content type 'application/zip' length 322953 bytes (315 KB) downloaded 315 KB package ‘aod’ successfully unpacked and MD5 sums checked The downloaded binary packages are in C:\Users\Bonnie\AppData\Local\Temp\Rtmpikl35B\downloaded_packages > sessionInfo() R version 3.6.1 (2019-07-05) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 17763) Matrix products: default Random number generation: RNG: Mersenne-Twister Normal: Inversion Sample: Rounding locale: [1] LC_COLLATE=Chinese (Simplified)_China.936 [2] LC_CTYPE=Chinese (Simplified)_China.936 [3] LC_MONETARY=Chinese (Simplified)_China.936 [4] LC_NUMERIC=C [5] LC_TIME=Chinese (Simplified)_China.936 attached base packages: [1] stats graphics grDevices utils [5] datasets methods base loaded via a namespace (and not attached): [1] compiler_3.6.1 tools_3.6.1 On 9/15/2019 11:02 PM, Duncan Murdoch wrote: > On 15/09/2019 1:44 a.m., Steven Yen wrote: >> Can someone help me understand why Rtools is needed when installing a >> package from CRAN, and from a zipped file? What's the point? > > Please don't just repeat your post when you've been asked for > additional supporting information. > > Duncan Murdoch > >> >> On 9/14/2019 10:29 PM, Steven Yen wrote: >>> Since updating to R3.6.1., I have received a WARNING message saying >>> Rtools is required. >>> I get the same message installing online from CRAN and from a .zip >>> file. >>> It looked like installation still went through in both cases, BUT, >>> >>> Another student installed a .zip file and received the following error >>> message: >>> >>> Error in install.packages : invalid multibyte string at `<87><55>... >>> >>> do I have to live with the message? >>> >>> I hate to tell students to install Rtools, which they do not do much >>> with. >>> Warning message is listed below. Thank you. >>> --- >>> >>>> Install.packages("aod") >>> WARNING: Rtools is required to build R packages but it is not >>> currently installed. Please download and install the appropriate >>> version of Rtools before proceeding: >>> >>> Package 'aod' successfully unpacked and MD5 sums checked. >>> -- >>> s...@hqu.edu.cn >> > -- st...@ntu.edu.tw (S.T. Yen) [[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] Warning from installing packages in R3.6.1
Can someone help me understand why Rtools is needed when installing a package from CRAN, and from a zipped file? What's the point? On 9/14/2019 10:29 PM, Steven Yen wrote: > Since updating to R3.6.1., I have received a WARNING message saying > Rtools is required. > I get the same message installing online from CRAN and from a .zip file. > It looked like installation still went through in both cases, BUT, > > Another student installed a .zip file and received the following error > message: > > Error in install.packages : invalid multibyte string at `<87><55>... > > do I have to live with the message? > > I hate to tell students to install Rtools, which they do not do much with. > Warning message is listed below. Thank you. > --- > > > Install.packages("aod") > WARNING: Rtools is required to build R packages but it is not > currently installed. Please download and install the appropriate > version of Rtools before proceeding: > > Package 'aod' successfully unpacked and MD5 sums checked. > -- > s...@hqu.edu.cn -- s...@hqu.edu.cn [[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] Warning from installing packages in R3.6.1
Since updating to R3.6.1., I have received a WARNING message saying Rtools is required. I get the same message installing online from CRAN and from a .zip file. It looked like installation still went through in both cases, BUT, Another student installed a .zip file and received the following error message: Error in install.packages : invalid multibyte string at `<87><55>... do I have to live with the message? I hate to tell students to install Rtools, which they do not do much with. Warning message is listed below. Thank you. --- > Install.packages("aod") WARNING: Rtools is required to build R packages but it is not currently installed. Please download and install the appropriate version of Rtools before proceeding: Package 'aod' successfully unpacked and MD5 sums checked. -- s...@hqu.edu.cn [[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] Plotting in R
I have a data frame containing two variables: year and rate (shown below). Which function can I use to plot rate (y-axis) against year (x-axis)? There will be more columns of rate later on. Thank you. year rate 1 1993 0.608 2 1994 0.622 3 1996 0.623 4 1998 0.647 5 2000 0.646 6 2002 0.625 7 2004 0.628 8 2006 0.685 9 2008 0.679 10 2010 0.595 11 2012 0.567 12 2014 0.599 13 2016 0.642 14 2018 0.685 --- This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus [[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] Plotting in R
I have a data frame containing two variables: year and rate (shown below). Which function can I use to plot rate (y-axis) against year (x-axis)? There will be more columns of rate later on. Thank you. year rate 1 1993 0.608 2 1994 0.622 3 1996 0.623 4 1998 0.647 5 2000 0.646 6 2002 0.625 7 2004 0.628 8 2006 0.685 9 2008 0.679 10 2010 0.595 11 2012 0.567 12 2014 0.599 13 2016 0.642 14 2018 0.685 -- st...@ntu.edu.tw (S.T. Yen) --- This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus [[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] Problem with random numbers/seed
Unhappy but thanks. Steven On 6/22/2019 1:13 AM, Uwe Ligges wrote: > > > On 21.06.2019 19:09, Steven Yen wrote: >> Now I see that results were replicated but running RNGversion >> I get a warning message. Isn't there a way to do this clean? > > Well, the old RNG is known to be not optimal, hence we give a warning > if you choose the old one. This is the clean way if you really want to > get the old (slightly buggy) behaviour. > > Best, > Uwe Ligges > >> >>> RNGversion("3.5.3") Warning message: In RNGkind("Mersenne-Twister", >> "Inversion", "Rounding") : non-uniform 'Rounding' sampler used >> >> >> On 6/22/2019 1:03 AM, Uwe Ligges wrote: >>> What does not work? >>> >>> >>> For me it works under R-3.6.0: >>> >>> x<-1:500 >>> set.seed(12345671) >>> j<-sample(1:length(x),size=60); y<-x[j] >>> summary(j) >>> >>> RNGversion("3.5.3") >>> set.seed(12345671) >>> j<-sample(1:length(x),size=60); y<-x[j] >>> summary(j) >>> >>> >>> Now I get the results you got udner the old R. >>> >>> Best, >>> Uwe Ligges >>> >>> >>> >>> On 21.06.2019 18:39, Steven Yen wrote: >>>> Thanks. Somewhat of a mystery. The older version I had was >>>> R-3.5.3patched. >>>> I cannot get the RNGversion command to run. Can you help? Thanks. >>>> >>>> On 6/22/2019 12:25 AM, Uwe Ligges wrote: >>>>> See the NEWS, the RNG has been changed, use RNGversion >>>>> >>>>> On 21.06.2019 18:10, Steven Yen wrote: >>>>>> Dear all, >>>>>> I did all this work with older R (R-3.5.3.patched and older) >>>>>> but now with R3.6 I cannot replicate the results. >>>>>> Below I sample 60 observations from 1:500 using the sample command >>>>>> with >>>>>> a random seed of 123. I get different results. Advice appreciated. >>>>>> Steven Yen >>>>>> >>>>>> > # Run under R-3.6.0 >>>>>> > x<-1:500 >>>>>> > set.seed(12345671) >>>>>> > j<-sample(1:length(x),size=60); y<-x[j] >>>>>> > summary(y) >>>>>> Min. 1st Qu. Median Mean 3rd Qu. Max. >>>>>> 26.0 134.2 240.0 249.8 368.0 500.0 >>>>>> >>>>>> > # Run under R-3.5.3.patched >>>>>> > x<-1:500 >>>>>> > set.seed(12345671) >>>>>> > j<-sample(1:length(x),size=60); y<-x[j] >>>>>> > summary(y) >>>>>> Min. 1st Qu. Median Mean 3rd Qu. Max. >>>>>> 9.0 122.2 205.0 236.1 364.2 493.0 >>>>>> >>>>> >>>>> >>>>> Under R-3.6.0 use, e.g. >>>>> RNGversion("3.5.2") >>>>> to get reproducible results from the older RNG. >>>>> >>>>> Best, >>>>> Uwe Ligges >>>>> >>>>> >>>> >>>> -- >>>> st...@ntu.edu.tw (S.T. Yen) >>>> >>> >> > -- st...@ntu.edu.tw (S.T. Yen) [[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] Problem with random numbers/seed
Now I see that results were replicated but running RNGversion I get a warning message. Isn't there a way to do this clean? > RNGversion("3.5.3") Warning message: In RNGkind("Mersenne-Twister", "Inversion", "Rounding") : non-uniform 'Rounding' sampler used On 6/22/2019 1:03 AM, Uwe Ligges wrote: > What does not work? > > > For me it works under R-3.6.0: > > x<-1:500 > set.seed(12345671) > j<-sample(1:length(x),size=60); y<-x[j] > summary(j) > > RNGversion("3.5.3") > set.seed(12345671) > j<-sample(1:length(x),size=60); y<-x[j] > summary(j) > > > Now I get the results you got udner the old R. > > Best, > Uwe Ligges > > > > On 21.06.2019 18:39, Steven Yen wrote: >> Thanks. Somewhat of a mystery. The older version I had was >> R-3.5.3patched. >> I cannot get the RNGversion command to run. Can you help? Thanks. >> >> On 6/22/2019 12:25 AM, Uwe Ligges wrote: >>> See the NEWS, the RNG has been changed, use RNGversion >>> >>> On 21.06.2019 18:10, Steven Yen wrote: >>>> Dear all, >>>> I did all this work with older R (R-3.5.3.patched and older) >>>> but now with R3.6 I cannot replicate the results. >>>> Below I sample 60 observations from 1:500 using the sample command >>>> with >>>> a random seed of 123. I get different results. Advice appreciated. >>>> Steven Yen >>>> >>>> > # Run under R-3.6.0 >>>> > x<-1:500 >>>> > set.seed(12345671) >>>> > j<-sample(1:length(x),size=60); y<-x[j] >>>> > summary(y) >>>> Min. 1st Qu. Median Mean 3rd Qu. Max. >>>> 26.0 134.2 240.0 249.8 368.0 500.0 >>>> >>>> > # Run under R-3.5.3.patched >>>> > x<-1:500 >>>> > set.seed(12345671) >>>> > j<-sample(1:length(x),size=60); y<-x[j] >>>> > summary(y) >>>> Min. 1st Qu. Median Mean 3rd Qu. Max. >>>> 9.0 122.2 205.0 236.1 364.2 493.0 >>>> >>> >>> >>> Under R-3.6.0 use, e.g. >>> RNGversion("3.5.2") >>> to get reproducible results from the older RNG. >>> >>> Best, >>> Uwe Ligges >>> >>> >> >> -- >> st...@ntu.edu.tw (S.T. Yen) >> > -- st...@ntu.edu.tw (S.T. Yen) [[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] Problem with random numbers/seed
Thanks. Somewhat of a mystery. The older version I had was R-3.5.3patched. I cannot get the RNGversion command to run. Can you help? Thanks. On 6/22/2019 12:25 AM, Uwe Ligges wrote: > See the NEWS, the RNG has been changed, use RNGversion > > On 21.06.2019 18:10, Steven Yen wrote: >> Dear all, >> I did all this work with older R (R-3.5.3.patched and older) >> but now with R3.6 I cannot replicate the results. >> Below I sample 60 observations from 1:500 using the sample command with >> a random seed of 123. I get different results. Advice appreciated. >> Steven Yen >> >> > # Run under R-3.6.0 >> > x<-1:500 >> > set.seed(12345671) >> > j<-sample(1:length(x),size=60); y<-x[j] >> > summary(y) >> Min. 1st Qu. Median Mean 3rd Qu. Max. >> 26.0 134.2 240.0 249.8 368.0 500.0 >> >> > # Run under R-3.5.3.patched >> > x<-1:500 >> > set.seed(12345671) >> > j<-sample(1:length(x),size=60); y<-x[j] >> > summary(y) >> Min. 1st Qu. Median Mean 3rd Qu. Max. >> 9.0 122.2 205.0 236.1 364.2 493.0 >> > > > Under R-3.6.0 use, e.g. > RNGversion("3.5.2") > to get reproducible results from the older RNG. > > Best, > Uwe Ligges > > -- st...@ntu.edu.tw (S.T. Yen) [[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] Problem with random numbers/seed
Dear all, I did all this work with older R (R-3.5.3.patched and older) but now with R3.6 I cannot replicate the results. Below I sample 60 observations from 1:500 using the sample command with a random seed of 123. I get different results. Advice appreciated. Steven Yen > # Run under R-3.6.0 > x<-1:500 > set.seed(12345671) > j<-sample(1:length(x),size=60); y<-x[j] > summary(y) Min. 1st Qu. Median Mean 3rd Qu. Max. 26.0 134.2 240.0 249.8 368.0 500.0 > # Run under R-3.5.3.patched > x<-1:500 > set.seed(12345671) > j<-sample(1:length(x),size=60); y<-x[j] > summary(y) Min. 1st Qu. Median Mean 3rd Qu. Max. 9.0 122.2 205.0 236.1 364.2 493.0 -- st...@ntu.edu.tw (S.T. Yen) [[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] Printing matrix/table in a procedure
Great! Thanks. On 5/15/2019 7:00 PM, Rui Barradas wrote: > Hello, > > What's wrong with > > test <- function(x){ > table <- matrix(x, nrow = 4) > cat("\nTable:\n") > print(table) > invisible(list(table = table)) > } > > > Hope this helps, > > Rui Barradas > > Às 10:33 de 15/05/19, Steven Yen escreveu: >> Dear All, >> >> I would like to get a matrix (table) printed in a procedure, as >> attempted below. Please help. Thanks. >> >> test<-function(x){ >> table<-matrix(x,nrow=4) >> cat("\nTable:\n",table) >> invisible(list(table=table)) >> } >> >> x<-1:20 >> test(x) >> > -- st...@ntu.edu.tw (S.T. Yen) [[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] Printing matrix/table in a procedure
Dear All, I would like to get a matrix (table) printed in a procedure, as attempted below. Please help. Thanks. test<-function(x){ table<-matrix(x,nrow=4) cat("\nTable:\n",table) invisible(list(table=table)) } x<-1:20 test(x) -- st...@ntu.edu.tw (S.T. Yen) [[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] Printing vectrix
I like to print a vector, wrapped by rows of 10. Below the first command below works for 20 numbers. The second command is ugly. How can I print the 25 numbers into 2 rows of ten plus a helf row of 5? Thanks. > x<-1:20; matrix(x,nrow=2,byrow=T) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 2 3 4 5 6 7 8 9 10 [2,] 11 12 13 14 15 16 17 18 19 20 > x<-1:25; matrix(x,nrow=2,byrow=T) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [1,] 1 2 3 4 5 6 7 8 9 10 11 12 13 [2,] 14 15 16 17 18 19 20 21 22 23 24 25 1 Warning message: In matrix(x, nrow = 2, byrow = T) : data length [25] is not a sub-multiple or multiple of the number of rows [2] > -- s...@hqu.edu.cn [[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] Installing package into...
I install package using either the command line or Tools -> Install packages... (in RStudio) and get a non-fatal message saying... Installing package into ‘C:/Users/xuhaer/Documents/R/win-library/3.5’ (as ‘lib’ is unspecified) I know it is not a fatal message. But, is there a way to do a cleaner installation without getting such message? Thanks. -- s...@hqu.edu.cn [[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] Retrievable results in a procedure
Thanks Sarah. Below, replacing "structure" with "invisible" does wonders--that serves my need. What I want is quite simple - I call a procedure and it does two things: (1) display results for all; (2) save retrievable results for use in further analysis, e.g., in knitr. Earlier, with "structure" (or with results<-list(...)) it spits out the main results, with components repeated (printed) in a painfully long list. Yet, as I said, calling with foo<-try(...) prints the main results with the list suppressed. I am just looking for option to NOT have to call with foo<- always. There must be more ways to do this, but I am happy with invisible. Thanks again. On 12/25/2018 11:10 PM, Sarah Goslee wrote: > I'm a bit confused about what you actually want, but I think > invisible() might be the answer. > > Note that there's already a base function try() so that's not a great > name for test functions. > > Sarah > > On Tue, Dec 25, 2018 at 8:47 AM Steven Yen <mailto:st...@ntu.edu.tw>> wrote: > > I would like to suppressed printing of retrievable results in a > procedure and to print only when retrieved. > > In line 10 below I call procedure "try" and get matrices A,B,C all > printed upon a call to the procedure. I get around this unwanted > printing by calling with v<-try(A,B) as in line 11. > > Any way to suppress printing of the retrievable results listed in the > structure command? Thank you, and Merry Christmas to all. > > > A<-matrix(rpois(16,lambda=5),nrow=4,byrow=T) > B<-diag(4) > > try<-function(A,B){ > C<-A+B > cat("\nC:\n"); print(C) > structure(list(A=A,B=B,C=C)) > } > > try(A,B)# line 10 > v<-try(A,B) # line 11 > > -- > st...@ntu.edu.tw <mailto:st...@ntu.edu.tw> (S.T. Yen) > > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org <mailto: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. > > -- > Sarah Goslee (she/her) > http://www.sarahgoslee.com -- st...@ntu.edu.tw (S.T. Yen) [[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] Retrievable results in a procedure
I would like to suppressed printing of retrievable results in a procedure and to print only when retrieved. In line 10 below I call procedure "try" and get matrices A,B,C all printed upon a call to the procedure. I get around this unwanted printing by calling with v<-try(A,B) as in line 11. Any way to suppress printing of the retrievable results listed in the structure command? Thank you, and Merry Christmas to all. A<-matrix(rpois(16,lambda=5),nrow=4,byrow=T) B<-diag(4) try<-function(A,B){ C<-A+B cat("\nC:\n"); print(C) structure(list(A=A,B=B,C=C)) } try(A,B)# line 10 v<-try(A,B) # line 11 -- st...@ntu.edu.tw (S.T. Yen) [[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] Random seed
I have known from the old days to set a random seed of a LARGE ODD NUMBER. Now I read instructions of set.seed and it requires ANY INTEGER. Any idea? Or, does it matter. Thanks. -- st...@ntu.edu.tw (S.T. Yen) [[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] Printing with cat in a procedure
Thank you all - print works wonders. On 12/22/2018 10:36 PM, Eric Berger wrote: > Hi Steven, > Here's one way, using print > > try5<-function(A,B){ > C<-A+B > #cat("\nA =",A,"\nC = ",C) > cat("\nA = ") > print(A) > cat("\nC = ") > print(C) > structure(list(A=A,B=B,C=C)) > } > > HTH, > Eric > > > On Sat, Dec 22, 2018 at 4:32 PM Steven Yen <mailto:st...@ntu.edu.tw>> wrote: > > How do I print a matrix running a procedure? In the code below, I > print > with the cat command and get a vector (from A and C). > > A<-matrix(rpois(16,lambda=5),nrow=4,byrow=T) > B<-diag(4) > > try5<-function(A,B){ > C<-A+B > cat("\nA =",A,"\nC = ",C) > structure(list(A=A,B=B,C=C)) > } > > v<-try5(A,B) > v$C > > -- > st...@ntu.edu.tw <mailto:st...@ntu.edu.tw> (S.T. Yen) > > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org <mailto: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. > -- st...@ntu.edu.tw (S.T. Yen) [[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] Printing with cat in a procedure
How do I print a matrix running a procedure? In the code below, I print with the cat command and get a vector (from A and C). A<-matrix(rpois(16,lambda=5),nrow=4,byrow=T) B<-diag(4) try5<-function(A,B){ C<-A+B cat("\nA =",A,"\nC = ",C) structure(list(A=A,B=B,C=C)) } v<-try5(A,B) v$C -- st...@ntu.edu.tw (S.T. Yen) [[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 apply
I need help with "apply". Below, I have no problem getting the column sums. 1. How do I get the sum of squares? 2. In general, where do I look up these functions? Thanks. x<-matrix(1:10,nrow=5); x sum <- apply(x,2,sum); sum [[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.