When assigning a dataframe to a subset of a matrix I got a very odd result. Am I missing something, or is this a bug? Details are below. Also, if m is defined outside of the current function, is m[...] <<- foo necessary to update it, or does regular replacement m[....] <- foo work (that is, does it update the "global" rather than creating a "local" that conceals it)? I got conflicting results, which were tangled up with the oddity below. -------------------------------------------------------- Browse[1]> covs epilepsy other.cancer 680 0 0 681 0 0 682 0 0 683 0 1 684 0 0 Browse[1]> m <- matrix(NA, 5,2) Browse[1]> m[,1:2] <- covs Browse[1]> m [[1]] [1] 0 0 0 0 0 [[2]] [1] 0 0 0 1 0 [[3]] [1] 0 0 0 0 0 [[4]] [1] 0 0 0 1 0 [[5]] [1] 0 0 0 0 0 [[6]] [1] 0 0 0 1 0 [[7]] [1] 0 0 0 0 0 [[8]] [1] 0 0 0 1 0 [[9]] [1] 0 0 0 0 0 [[10]] [1] 0 0 0 1 0 Browse[1]> dim(covs) [1] 5 2 Browse[1]> class(covs) [1] "data.frame" Browse[1]> class(m) [1] "list" Browse[1]> length(m) [1] 10 Fortunately, the following seems to work as expected: Browse[1]> m[,1:2] <- as.matrix(covs) Ross Boylan wk: (415) 502-4031 530 Parnassus Avenue (Library) rm 115-4 ross at biostat.ucsf.edu Dept of Epidemiology and Biostatistics fax: (415) 476-9856 University of California, San Francisco San Francisco, CA 94143-0840 hm: (415) 550-1062
A subset of a data.frame is still a data.frame. A data.frame is actually a list with additional attributes. As far as I know, your solution, "as.matrix", is an appropriate tool to convert a data.frame to a matrix. Caution may be appropropriate, however, because if the data.frame includes anything not numeric, the result may not be numeric, and the result is inconsistent between S-Plus 6.1 and R 1.7.1. Consider the following: S-PLUS 6.1: > DF <- data.frame(a = 1:2, b = letters[1:2]) > DF2 <- data.frame(c = TRUE) > DF3 <- data.frame(c = TRUE, d = 2) > sapply(DF, class) a b "integer" "factor" > as.matrix(DF) a b 1 "1" "a" 2 "2" "b" > as.matrix(DF2) c 1 T > as.matrix(DF3) c d 1 1 2 ################## R 1.7.1: > DF <- data.frame(a=1:2, b=letters[1:2]) > DF2 <- data.frame(c=TRUE) > DF3 <- data.frame(c=TRUE, d=2) > sapply(DF, class) a b "integer" "factor" > as.matrix(DF) a b 1 "1" "a" 2 "2" "b" > as.matrix(DF2) c 1 "TRUE" > as.matrix(DF3) c d 1 "TRUE" "2" ##################### Hope this helps. spencer graves Ross Boylan wrote:>When assigning a dataframe to a subset of a matrix I got a very odd >result. Am I missing something, or is this a bug? Details are below. > >Also, if m is defined outside of the current function, is >m[...] <<- foo >necessary to update it, or does regular replacement >m[....] <- foo >work (that is, does it update the "global" rather than creating a >"local" that conceals it)? I got conflicting results, which were >tangled up with the oddity below. >-------------------------------------------------------- > >Browse[1]> covs > epilepsy other.cancer >680 0 0 >681 0 0 >682 0 0 >683 0 1 >684 0 0 >Browse[1]> m <- matrix(NA, 5,2) >Browse[1]> m[,1:2] <- covs >Browse[1]> m >[[1]] >[1] 0 0 0 0 0 > >[[2]] >[1] 0 0 0 1 0 > >[[3]] >[1] 0 0 0 0 0 > >[[4]] >[1] 0 0 0 1 0 > >[[5]] >[1] 0 0 0 0 0 > >[[6]] >[1] 0 0 0 1 0 > >[[7]] >[1] 0 0 0 0 0 > >[[8]] >[1] 0 0 0 1 0 > >[[9]] >[1] 0 0 0 0 0 > >[[10]] >[1] 0 0 0 1 0 > >Browse[1]> dim(covs) >[1] 5 2 >Browse[1]> class(covs) >[1] "data.frame" >Browse[1]> class(m) >[1] "list" >Browse[1]> length(m) >[1] 10 > >Fortunately, the following seems to work as expected: >Browse[1]> m[,1:2] <- as.matrix(covs) > > >Ross Boylan wk: (415) 502-4031 >530 Parnassus Avenue (Library) rm 115-4 ross at biostat.ucsf.edu >Dept of Epidemiology and Biostatistics fax: (415) 476-9856 >University of California, San Francisco >San Francisco, CA 94143-0840 hm: (415) 550-1062 > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help > >
On Wed, Sep 17, 2003 at 05:41:09PM -0700, Spencer Graves wrote:> A subset of a data.frame is still a data.frame. A data.frame is > actually a list with additional attributes. As far as I know, your > solution, "as.matrix", is an appropriate tool to convert a data.frame to > a matrix. Caution may be appropropriate, however, because if theThat helps explain where the list assignment is coming from, but it still seems very odd that assignment to a subset of a matrix should change the class of the object, so that it is no longer a matrix.
>>>>> "Spencer" == Spencer Graves <spencer.graves at pdf.com> >>>>> on Wed, 17 Sep 2003 17:41:09 -0700 writes:Spencer> A subset of a data.frame is still a data.frame. A Spencer> data.frame is actually a list with additional Spencer> attributes. As far as I know, your solution, Spencer> "as.matrix", is an appropriate tool to convert a Spencer> data.frame to a matrix. Caution may be Spencer> appropropriate, however, because if the data.frame Spencer> includes anything not numeric, the result may not Spencer> be numeric, and the result is inconsistent between Spencer> S-Plus 6.1 and R 1.7.1. Yes, using "as.matrix" is typically not the best, but rather data.matrix() ------------ is what you should apply usually to data frames. (and which is a bit more consistent between R and S-plus).