Re: [R] How to extract convergence code from lmer object?
Juan Andres Hernandez gmail.com> writes: > > Does anyone know how to extract the convergence code of an lmer object. I > am working in a monte carlo simulation with mixed model and I need to know > if a model has or not convergence. With unclass(mymodel) the following > information attr(,"optinfo")$conv$lme4 can be seen. How can I get this > important information in an automatic way? I don't know what you mean by "automatic". mymodel@optinfo$conv$lme4 is the way that I usually extract it. Yes, there should be a better accessor method. Follow-ups to r-sig-mixed-mod...@r-project.org, please ... __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Predictions from "coxph" or "cph" objects
On Jul 5, 2014, at 9:12 PM, David Winsemius wrote: On Jul 5, 2014, at 12:43 PM, Axel Urbiz wrote: Thank you David. It is my understanding that using survfirsurvit below I get the median predicted survival. I actually was looking for the mean. I can't seem to find in the documentation how to get that. options(na.action=na.exclude) # retain NA in predictions fit <- coxph(Surv(time, status) ~ age + ph.ecog, lung) pred <- survfit(fit, newdata=lung) head(pred) There might be a way. I don't know it if so, so I would probably just use the definition of the mean: sum(summary(pred)$surv* summary(pred)$time)/sum( summary(pred)$time) Er, I think I meant to type: fit <- coxph(Surv(time, status) ~ age + ph.ecog, lung) pred <- survfit(fit) sum(summary(pred)$surv* summary(pred)$time)/sum( summary(pred)$surv) [1] 211.0943 (I continue to take effort to keep my postings in plain text despite my mail-clients's efforts to match your formatted postings. It adds to the work of responders when you post formatted questions and responses.) Thanks again, Axel. On Sat, Jul 5, 2014 at 1:54 PM, David Winsemius > wrote: On Jul 5, 2014, at 5:28 AM, Axel Urbiz wrote: Dear R users, My apologies for the simple question, as I'm starting to learn the concepts behind the Cox PH model. I was just experimenting with the survival and rms packages for this. I'm simply trying to obtain the expected survival time (as opposed to the probability of survival at a given time t). What does "expected survival time" actually mean? Do you want the median survival time? I can't seem to find an option from the "type" argument in the predict methods from coxph{survival} or cph{rms} that will give me expected survival times. library(rms) options(na.action=na.exclude) # retain NA in predictions fit <- coxph(Surv(time, status) ~ age + ph.ecog, lung) fit2 <- cph(Surv(time, status) ~ age + ph.ecog, lung) head(predict(fit,type="lp")) head(predict(fit2,type="lp")) `predict` will return the results of the regression, i.e. the log- hazard ratios for each term in the RHS of the formula. What you want (as described in the Index for the survival package) is either `survfit` or `survexp`. require(survival) help(pack=survival) ?survfit ?survexp ?summary.survfit ?quantile.survfit # to get the median ?print.summary.survfit require(rms) help(pack=rms) The rms-package also adds a `survfit.cph` function but I have found the `survest` function also provides useful added features, beyond those offered by survfit Thank you. Regards, Axel. [[alternative HTML version deleted]] This is a plain text mailing list. -- David Winsemius, MD Alameda, CA, USA David Winsemius, MD Alameda, CA, USA __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. David Winsemius, MD Alameda, CA, USA __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Predictions from "coxph" or "cph" objects
On Jul 5, 2014, at 12:43 PM, Axel Urbiz wrote: Thank you David. It is my understanding that using survfirsurvit below I get the median predicted survival. I actually was looking for the mean. I can't seem to find in the documentation how to get that. options(na.action=na.exclude) # retain NA in predictions fit <- coxph(Surv(time, status) ~ age + ph.ecog, lung) pred <- survfit(fit, newdata=lung) head(pred) There might be a way. I don't know it if so, so I would probably just use the definition of the mean: sum(summary(pred)$surv* summary(pred)$time)/sum( summary(pred)$time) (I continue to take effort to keep my postings in plain text despite my mail-clients's efforts to match your formatted postings. It adds to the work of responders when you post formatted questions and responses.) Thanks again, Axel. On Sat, Jul 5, 2014 at 1:54 PM, David Winsemius > wrote: On Jul 5, 2014, at 5:28 AM, Axel Urbiz wrote: Dear R users, My apologies for the simple question, as I'm starting to learn the concepts behind the Cox PH model. I was just experimenting with the survival and rms packages for this. I'm simply trying to obtain the expected survival time (as opposed to the probability of survival at a given time t). What does "expected survival time" actually mean? Do you want the median survival time? I can't seem to find an option from the "type" argument in the predict methods from coxph{survival} or cph{rms} that will give me expected survival times. library(rms) options(na.action=na.exclude) # retain NA in predictions fit <- coxph(Surv(time, status) ~ age + ph.ecog, lung) fit2 <- cph(Surv(time, status) ~ age + ph.ecog, lung) head(predict(fit,type="lp")) head(predict(fit2,type="lp")) `predict` will return the results of the regression, i.e. the log- hazard ratios for each term in the RHS of the formula. What you want (as described in the Index for the survival package) is either `survfit` or `survexp`. require(survival) help(pack=survival) ?survfit ?survexp ?summary.survfit ?quantile.survfit # to get the median ?print.summary.survfit require(rms) help(pack=rms) The rms-package also adds a `survfit.cph` function but I have found the `survest` function also provides useful added features, beyond those offered by survfit Thank you. Regards, Axel. [[alternative HTML version deleted]] This is a plain text mailing list. -- David Winsemius, MD Alameda, CA, USA David Winsemius, MD Alameda, CA, USA __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Predictions from "coxph" or "cph" objects
Thank you David. It is my understanding that using survit below I get the median predicted survival. I actually was looking for the mean. I can't seem to find in the documentation how to get that. options(na.action=na.exclude) # retain NA in predictions fit <- coxph(Surv(time, status) ~ age + ph.ecog, lung) pred <- survfit(fit, newdata=lung) head(pred) Thanks again, Axel. On Sat, Jul 5, 2014 at 1:54 PM, David Winsemius wrote: > > On Jul 5, 2014, at 5:28 AM, Axel Urbiz wrote: > > Dear R users, >> >> My apologies for the simple question, as I'm starting to learn the >> concepts >> behind the Cox PH model. I was just experimenting with the survival and >> rms >> packages for this. >> >> I'm simply trying to obtain the expected survival time (as opposed to the >> probability of survival at a given time t). >> > > What does "expected survival time" actually mean? Do you want the median > survival time? > > > I can't seem to find an option >> from the "type" argument in the predict methods from coxph{survival} or >> cph{rms} that will give me expected survival times. >> >> library(rms) >> options(na.action=na.exclude) # retain NA in predictions >> fit <- coxph(Surv(time, status) ~ age + ph.ecog, lung) >> fit2 <- cph(Surv(time, status) ~ age + ph.ecog, lung) >> head(predict(fit,type="lp")) >> head(predict(fit2,type="lp")) >> > > `predict` will return the results of the regression, i.e. the log-hazard > ratios for each term in the RHS of the formula. What you want (as described > in the Index for the survival package) is either `survfit` or `survexp`. > > require(survival) > help(pack=survival) > ?survfit > ?survexp > ?summary.survfit > ?quantile.survfit # to get the median > ?print.summary.survfit > > require(rms) > help(pack=rms) > > The rms-package also adds a `survfit.cph` function but I have found the > `survest` function also provides useful added features, beyond those > offered by survfit > > >> >> Thank you. >> >> Regards, >> Axel. >> >> [[alternative HTML version deleted]] >> > > This is a plain text mailing list. > > -- > > David Winsemius, MD > Alameda, CA, USA > > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Best practice: to factor or not to factor for float variables
However, > format((0.1+0.2)) == format(0.3) [1] TRUE Which suggests that if you want to treat measured variables as categories, one way to do it is to format them first. Of course, one may have to control the format more carefully than above (if necessary, see for example ?formatC). merge() on carefully formatted float values may be more reliable than merging on the floats themselves. -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 7/4/14 4:04 AM, "Sebastian Schubert" wrote: >Hi, > >I would like to ask for best practice advice on the design of data >structure and the connected analysis techniques. > >In my particular case, I have measurements of several variables at >several, sometimes equal, heights. Following the tidy data approach of >Hadley Wickham, I want to put all data in one data frame. In principle, >the height variable is something like a category. For example, I want to >average over time for every height. Using dplyr this works very well >when my height variable is a factor. However, if it is not a factor the >grouping sometimes will not work probably due to numerical issues: > >http://stackoverflow.com/questions/24555010/dplyr-and-group-by-factor-vs-n >o-factor >https://github.com/hadley/dplyr/issues/482 > >Even if the behaviour described in the links above is a bug, on can >easily create other numerical issues in R: >> (0.1+0.2) == 0.3 >[1] FALSE > >Thus, it seems one should avoid grouping by float values and, in my >case, use factors. However, from time to time, I need the numerical >character of the heights: compare heights, find the maximum height, etc. >Here, the ordered factor approach might help. However, I have to combine >(via rbind or merge) different data sets quite often so keeping the >order of the different ordered factor heights also seem to be difficult. > >Is there any general approach which reduces the work or do I have to >switch between approaches as needed? > >Thanks a lot for any input, >Sebastian > >__ >R-help@r-project.org mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Predictions from "coxph" or "cph" objects
On Jul 5, 2014, at 5:28 AM, Axel Urbiz wrote: Dear R users, My apologies for the simple question, as I'm starting to learn the concepts behind the Cox PH model. I was just experimenting with the survival and rms packages for this. I'm simply trying to obtain the expected survival time (as opposed to the probability of survival at a given time t). What does "expected survival time" actually mean? Do you want the median survival time? I can't seem to find an option from the "type" argument in the predict methods from coxph{survival} or cph{rms} that will give me expected survival times. library(rms) options(na.action=na.exclude) # retain NA in predictions fit <- coxph(Surv(time, status) ~ age + ph.ecog, lung) fit2 <- cph(Surv(time, status) ~ age + ph.ecog, lung) head(predict(fit,type="lp")) head(predict(fit2,type="lp")) `predict` will return the results of the regression, i.e. the log- hazard ratios for each term in the RHS of the formula. What you want (as described in the Index for the survival package) is either `survfit` or `survexp`. require(survival) help(pack=survival) ?survfit ?survexp ?summary.survfit ?quantile.survfit # to get the median ?print.summary.survfit require(rms) help(pack=rms) The rms-package also adds a `survfit.cph` function but I have found the `survest` function also provides useful added features, beyond those offered by survfit Thank you. Regards, Axel. [[alternative HTML version deleted]] This is a plain text mailing list. -- David Winsemius, MD Alameda, CA, USA __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Using R to analyze multiple MRI studies
Did you inspect the CRAN view for Medical imaging? http://cran.r-project.org/web/views/MedicalImaging.html On 3 July 2014 17:09, moleps islon wrote: > I need to analyze multiple T1 contrast enhanced MRI studies from different > patients. They are all in DICOM format. I see that there are different > packages for loading individual studies in DICOM format, however I have had > limited luck so far researching how the different studies can be tranformed > into MNI or Talairach space. Is there an R-implementation of this? > > Best, > > M > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Using subplot (from Hmisc) along with par(mfrow)
The subplot function in the TeachingDemos package is more up to date than the version in Hmisc (the Hmisc version is a copy of an earlier version of the one in TeachingDemos). If you replace library(Hmisc) with library(TeachingDemos) (with a recent version of TeachingDemos installed) then the code works as you expect. On Thu, Jul 3, 2014 at 1:45 PM, Cory Champagne wrote: > >Hello all, >I think this should be a relatively easy solution involving par but I can't >figure it out, involving subplot: >I'm making a three-figure plot, each with a subplot. Here's simple >reproducible code below. But each plot seems to call the original par >setting and redraws the new plot in the first position, rather than adding >subsequent plots in a single plot window. >Can someone tell me how to fix this so the result is three figures, each >containing a subplot, all within a single plot window? >Thanks, >-Cory >library(Hmisc)# subplot from the Hmisc package >par(mfrow=c(3,1) )# set mfrow for 3 rows and 1 column. >plot(1:10, 1:10, main = "Plot 1") >subplot(plot(10,10, xlab="", ylab=""), x=2, y=8, size = c(0.5, 0.5) ) >plot(11:20, 11:20, main = "Plot 2") >subplot(plot(10,10, xlab="", ylab=""), x=12, y=18, size = c(0.5, 0.5) >) >plot(21:30, 21:30, main = "Plot 3") >subplot(plot(10,10, xlab="", ylab=""), x=22, y=28, size = c(0.5, 0.5) >) > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. -- Gregory (Greg) L. Snow Ph.D. 538...@gmail.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] metafor package: changing decimal in forest plot to midline decimal
I found this: https://stat.ethz.ch/pipermail/r-help/2012-August/321057.html So, use this before drawing the forest plot: options(OutDec="\xB7") Best, Wolfgang -- Wolfgang Viechtbauer, Ph.D., Statistician Department of Psychiatry and Psychology School for Mental Health and Neuroscience Faculty of Health, Medicine, and Life Sciences Maastricht University, P.O. Box 616 (VIJV1) 6200 MD Maastricht, The Netherlands +31 (43) 388-4170 | http://www.wvbauer.com From: r-help-boun...@r-project.org [r-help-boun...@r-project.org] On Behalf Of Dietlinde Schmidt [schmidt.dietli...@web.de] Sent: Thursday, July 03, 2014 3:07 PM To: r-help@r-project.org Subject: [R] metafor package: changing decimal in forest plot to midline decimal Dear R-Community, I need to change the punctuation of the reported weights, effect sizes and confidence intervals in a forest plot created with the forest()-function in the metafor-package. Midline decimal means that it looks like this (23·6) rather than that (23.6). Do I need to change the forest()-function and if yes which part exactly? Or is there an otherway how I can do it maybe by changing the rma()-function, of which the forest()-function is then applied to? Thanks for any hints and tipps! Cheers, Linde __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] how does a valid subscript can produce an "subscript out of bounds" error?
I think you are somewhere between Circle 8.2.6 of 'The R Inferno' http://www.burns-stat.com/documents/books/the-r-inferno/ and the basics of subscripting http://www.burns-stat.com/documents/tutorials/impatient-r/more-r-subscript/ Pat On 05/07/2014 15:19, Witold E Wolski wrote: Thank you. You example helped to FIX IT. The problem is I guess somehow related to: class(msexp$pepinfo$transition_group_id) [1] "factor" and the whole R type conversion , riddle. For subscripts my intuition is: either require integer or do the "cast" to the rowname type (character). However, it seems that R somehow prefers to cast factors to integers... it seems that %in% "casts" both vectors to the same type. But to which one? Oh, I guess all this is neatly explained in the R standard ... but websearching for it just returns: standard deviation a frustrated R user. On 5 July 2014 02:18, Duncan Murdoch wrote: On 04/07/2014, 6:35 PM, Witold E Wolski wrote: how does a valid subscript (see first 2 lines) can produce an "subscript out of bounds" error (see line 4)? 1> sum(!rownames(msexp$rt) %in% msexp$pepinfo$transition_group_id) [1] 0 2> sum(!msexp$pepinfo$transition_group_id %in% rownames(msexp$rt)) [1] 0 3> class(msexp$rt) [1] "matrix" 4> msexp$rt = as.matrix(msexp$rt[msexp$pepinfo$transition_group_id,]) Error in msexp$rt[msexp$pepinfo$transition_group_id, ] : subscript out of bounds x <- matrix(1,1,1) rownames(x) <- colnames(x) <- "23" 23 %in% rownames(x) [1] TRUE x[23,] Error in x[23, ] : subscript out of bounds -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] how does a valid subscript can produce an "subscript out of bounds" error?
On 05 Jul 2014, at 16:19 , Witold E Wolski wrote: > Thank you. You example helped to FIX IT. > > The problem is I guess somehow related to: >> class(msexp$pepinfo$transition_group_id) > [1] "factor" > > and the whole R type conversion , riddle. > > For subscripts my intuition is: either require integer or do the > "cast" to the rowname type (character). > However, it seems that R somehow prefers to cast factors to integers... > > it seems that %in% "casts" both vectors to the same type. But to which one? > > Oh, I guess all this is neatly explained in the R standard ... but > websearching for it just returns: > standard deviation > >From help("[") The index object ‘i’ can be numeric, logical, character or empty. Indexing by factors is allowed and is equivalent to indexing by the numeric codes (see ‘factor’) and not by the character values which are printed (for which use ‘[as.character(i)]’). One reason for hanging on to this convention is that it allows you to do plot(x, y, col=c("red","blue")[sex]) Another reason is that it is the broader definition: It works for indexing vectors that do not have names. > > a frustrated R user. > > On 5 July 2014 02:18, Duncan Murdoch wrote: >> On 04/07/2014, 6:35 PM, Witold E Wolski wrote: >>> how does a valid subscript (see first 2 lines) can produce an >>> "subscript out of bounds" error (see line 4)? >>> >>> >>> 1> sum(!rownames(msexp$rt) %in% msexp$pepinfo$transition_group_id) >>> [1] 0 >>> 2> sum(!msexp$pepinfo$transition_group_id %in% rownames(msexp$rt)) >>> [1] 0 >>> 3> class(msexp$rt) >>> [1] "matrix" >>> 4> msexp$rt = as.matrix(msexp$rt[msexp$pepinfo$transition_group_id,]) >>> Error in msexp$rt[msexp$pepinfo$transition_group_id, ] : >>> subscript out of bounds >>> >> >>> x <- matrix(1,1,1) >>> rownames(x) <- colnames(x) <- "23" >>> 23 %in% rownames(x) >> [1] TRUE >>> x[23,] >> Error in x[23, ] : subscript out of bounds >> > > > > -- > Witold Eryk Wolski > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd@cbs.dk Priv: pda...@gmail.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] how does a valid subscript can produce an "subscript out of bounds" error?
Thank you. You example helped to FIX IT. The problem is I guess somehow related to: > class(msexp$pepinfo$transition_group_id) [1] "factor" and the whole R type conversion , riddle. For subscripts my intuition is: either require integer or do the "cast" to the rowname type (character). However, it seems that R somehow prefers to cast factors to integers... it seems that %in% "casts" both vectors to the same type. But to which one? Oh, I guess all this is neatly explained in the R standard ... but websearching for it just returns: standard deviation a frustrated R user. On 5 July 2014 02:18, Duncan Murdoch wrote: > On 04/07/2014, 6:35 PM, Witold E Wolski wrote: >> how does a valid subscript (see first 2 lines) can produce an >> "subscript out of bounds" error (see line 4)? >> >> >> 1> sum(!rownames(msexp$rt) %in% msexp$pepinfo$transition_group_id) >> [1] 0 >> 2> sum(!msexp$pepinfo$transition_group_id %in% rownames(msexp$rt)) >> [1] 0 >> 3> class(msexp$rt) >> [1] "matrix" >> 4> msexp$rt = as.matrix(msexp$rt[msexp$pepinfo$transition_group_id,]) >> Error in msexp$rt[msexp$pepinfo$transition_group_id, ] : >> subscript out of bounds >>> >> > >> x <- matrix(1,1,1) >> rownames(x) <- colnames(x) <- "23" >> 23 %in% rownames(x) > [1] TRUE >> x[23,] > Error in x[23, ] : subscript out of bounds > -- Witold Eryk Wolski __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Predictions from "coxph" or "cph" objects
Dear R users, My apologies for the simple question, as I'm starting to learn the concepts behind the Cox PH model. I was just experimenting with the survival and rms packages for this. I'm simply trying to obtain the expected survival time (as opposed to the probability of survival at a given time t). I can't seem to find an option from the "type" argument in the predict methods from coxph{survival} or cph{rms} that will give me expected survival times. library(rms) options(na.action=na.exclude) # retain NA in predictions fit <- coxph(Surv(time, status) ~ age + ph.ecog, lung) fit2 <- cph(Surv(time, status) ~ age + ph.ecog, lung) head(predict(fit,type="lp")) head(predict(fit2,type="lp")) Thank you. Regards, Axel. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Calling Matrices from a Function
Hi, This line needs double brackets: result_1 <- (result[[1]]) With single brackets you're keeping the list object. With double brackets you're extracting just the object within the list. Sarah On Friday, July 4, 2014, Cheryl Johnson wrote: > When I call matrices from a function, they are called but are not > recognized as matrices. I use the code below. From the main function, when > I use the command is.matrix(), the response is FALSE. Why are the matrices > not recognized as matrices? Thanks in advance for any guidance. > > #This function creates matrices and returns them in a list. > > my_func <- function() { > > matrix_1 = matrix(c(1,2,3,4), ncol = 2, nrow = 2) > > matrix_2 = matrix(c(5,6,7,8), ncol = 2, nrow = 2) > > my_list <- list(matrix_1,matrix_2) > > return(my_list) > > } > > #This function calls the myfunc function. > > main_func <- function() { > > result <- myfunc() > > result_1 <- (result[1]) > > print(result_1) > > print(is.matrix(a)) > > result_2 <- (result[2]) > > print(result_2) > > print(is.matrix(result_2)) > > } > > main_func() > > > #This function creates matrices and returns them in a list. > > > my_func <- function() { > > + matrix_1 = matrix(c(1,2,3,4), ncol = 2, nrow = 2) > > + matrix_2 = matrix(c(5,6,7,8), ncol = 2, nrow = 2) > > + my_list <- list(matrix_1,matrix_2) > > + return(my_list) > > + } > > > #This function calls the myfunc function. > > > main_func <- function() { > > + result <- myfunc() > > + result_1 <- (result[1]) > > + print(result_1) > > + print(is.matrix(a)) > > + result_2 <- (result[2]) > > + print(result_2) > > + print(is.matrix(result_2)) > > + } > > > main_func() > > [[1]] > > [,1] [,2] > > [1,]13 > > [2,]24 > > > > [1] FALSE > > [[1]] > > [,1] [,2] > > [1,]57 > > [2,]68 > > > > [1] FALSE > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > -- Sarah Goslee http://www.stringpage.com http://www.sarahgoslee.com http://www.functionaldiversity.org [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.