If you look at the structure, you'll see:> x$V4 <- 0 > str(x)`data.frame': 4 obs. of 4 variables: $ V1: int 1 2 3 4 $ V2: int 5 6 7 8 $ V3: int 9 10 11 12 $ V4: num 0 Don't know if this is the intended result. In any case, you're probably better off using data.matrix, as> data.matrix(x)V1 V2 V3 V4 1 1 5 9 0 2 2 6 10 0 3 3 7 11 0 4 4 8 12 0 HTH, Andy> -----Original Message----- > From: Alberto Murta [mailto:amurta at ipimar.pt] > Sent: Thursday, August 14, 2003 12:50 PM > To: r-help at stat.math.ethz.ch > Subject: [R] vectorization question > > > Dear all > > I recently noticed the following error when cohercing a > data.frame into a > matrix: > > > example <- matrix(1:12,4,3) > > example <- as.data.frame(example) > > example$V4 <- 0 > > example > V1 V2 V3 V4 > 1 1 5 9 0 > 2 2 6 10 0 > 3 3 7 11 0 > 4 4 8 12 0 > > example <- as.matrix(example) > Error in as.matrix.data.frame(example) : dim<- length of dims > do not match the > length of object > > However, if the column to be added has the right number of > lines, there's no > error: > > > example <- matrix(1:12,4,3) > > example <- as.data.frame(example) > > example$V4 <- rep(0,4) > > example > V1 V2 V3 V4 > 1 1 5 9 0 > 2 2 6 10 0 > 3 3 7 11 0 > 4 4 8 12 0 > > example <- as.matrix(example) > > example > V1 V2 V3 V4 > 1 1 5 9 0 > 2 2 6 10 0 > 3 3 7 11 0 > 4 4 8 12 0 > > Shouldn't it work well both ways? I checked the attributes > and dims of the > data frame and they are the same in both cases. Where's the > difference that > originates the error message? > Thanks in advance > > Alberto > > platform i686-pc-linux-gnu > arch i686 > os linux-gnu > system i686, linux-gnu > status > major 1 > minor 7.1 > year 2003 > month 06 > day 16 > language R > > > -- > Alberto G. Murta > Institute for Agriculture and Fisheries Research (INIAP-IPIMAR) > Av. Brasilia, 1449-006 Lisboa, Portugal | Phone: +351 > 213027062 Fax:+351 213015948 | > http://www.ipimar-iniap.ipimar.pt/pelagicos/ > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo> /r-help >------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments, contains information of Merck & Co., Inc. (Whitehouse Station, New Jersey, USA), and/or its affiliates (which may be known outside the United States as Merck Frosst, Merck Sharp & Dohme or MSD) that may be confidential, proprietary copyrighted and/or legally privileged, and is intended solely for the use of the individual or entity named on this message. If you are not the intended recipient, and have received this message in error, please immediately return this by e-mail and then delete it.
Dear all I recently noticed the following error when cohercing a data.frame into a matrix:> example <- matrix(1:12,4,3) > example <- as.data.frame(example) > example$V4 <- 0 > exampleV1 V2 V3 V4 1 1 5 9 0 2 2 6 10 0 3 3 7 11 0 4 4 8 12 0> example <- as.matrix(example)Error in as.matrix.data.frame(example) : dim<- length of dims do not match the length of object However, if the column to be added has the right number of lines, there's no error:> example <- matrix(1:12,4,3) > example <- as.data.frame(example) > example$V4 <- rep(0,4) > exampleV1 V2 V3 V4 1 1 5 9 0 2 2 6 10 0 3 3 7 11 0 4 4 8 12 0> example <- as.matrix(example) > exampleV1 V2 V3 V4 1 1 5 9 0 2 2 6 10 0 3 3 7 11 0 4 4 8 12 0 Shouldn't it work well both ways? I checked the attributes and dims of the data frame and they are the same in both cases. Where's the difference that originates the error message? Thanks in advance Alberto platform i686-pc-linux-gnu arch i686 os linux-gnu system i686, linux-gnu status major 1 minor 7.1 year 2003 month 06 day 16 language R -- Alberto G. Murta Institute for Agriculture and Fisheries Research (INIAP-IPIMAR) Av. Brasilia, 1449-006 Lisboa, Portugal | Phone: +351 213027062 Fax:+351 213015948 | http://www.ipimar-iniap.ipimar.pt/pelagicos/
Thank you very much. I just would expect that 'as.matrix' would have the same behaviour as 'data.matrix' when all columns in a data frame are numeric. Regards Alberto On Thursday 14 August 2003 16:41, Liaw, Andy wrote:> If you look at the structure, you'll see: > > x$V4 <- 0 > > str(x) > > `data.frame': 4 obs. of 4 variables: > $ V1: int 1 2 3 4 > $ V2: int 5 6 7 8 > $ V3: int 9 10 11 12 > $ V4: num 0 > > Don't know if this is the intended result. In any case, you're probably > better off using data.matrix, as > > > data.matrix(x) > > V1 V2 V3 V4 > 1 1 5 9 0 > 2 2 6 10 0 > 3 3 7 11 0 > 4 4 8 12 0 > > HTH, > Andy > > > -----Original Message----- > > From: Alberto Murta [mailto:amurta at ipimar.pt] > > Sent: Thursday, August 14, 2003 12:50 PM > > To: r-help at stat.math.ethz.ch > > Subject: [R] vectorization question > > > > > > Dear all > > > > I recently noticed the following error when cohercing a > > data.frame into a > > > > matrix: > > > example <- matrix(1:12,4,3) > > > example <- as.data.frame(example) > > > example$V4 <- 0 > > > example > > > > V1 V2 V3 V4 > > 1 1 5 9 0 > > 2 2 6 10 0 > > 3 3 7 11 0 > > 4 4 8 12 0 > > > > > example <- as.matrix(example) > > > > Error in as.matrix.data.frame(example) : dim<- length of dims > > do not match the > > length of object > > > > However, if the column to be added has the right number of > > lines, there's no > > > > error: > > > example <- matrix(1:12,4,3) > > > example <- as.data.frame(example) > > > example$V4 <- rep(0,4) > > > example > > > > V1 V2 V3 V4 > > 1 1 5 9 0 > > 2 2 6 10 0 > > 3 3 7 11 0 > > 4 4 8 12 0 > > > > > example <- as.matrix(example) > > > example > > > > V1 V2 V3 V4 > > 1 1 5 9 0 > > 2 2 6 10 0 > > 3 3 7 11 0 > > 4 4 8 12 0 > > > > Shouldn't it work well both ways? I checked the attributes > > and dims of the > > data frame and they are the same in both cases. Where's the > > difference that > > originates the error message? > > Thanks in advance > > > > Alberto > > > > platform i686-pc-linux-gnu > > arch i686 > > os linux-gnu > > system i686, linux-gnu > > status > > major 1 > > minor 7.1 > > year 2003 > > month 06 > > day 16 > > language R > > > > > > -- > > Alberto G. Murta > > Institute for Agriculture and Fisheries Research (INIAP-IPIMAR) > > Av. Brasilia, 1449-006 Lisboa, Portugal | Phone: +351 > > 213027062 Fax:+351 213015948 | > > http://www.ipimar-iniap.ipimar.pt/pelagicos/ > > > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://www.stat.math.ethz.ch/mailman/listinfo> /r-help > > --------------------------------------------------------------------------- >--- Notice: This e-mail message, together with any attachments, contains > information of Merck & Co., Inc. (Whitehouse Station, New Jersey, USA), > and/or its affiliates (which may be known outside the United States as > Merck Frosst, Merck Sharp & Dohme or MSD) that may be confidential, > proprietary copyrighted and/or legally privileged, and is intended solely > for the use of the individual or entity named on this message. If you are > not the intended recipient, and have received this message in error, please > immediately return this by e-mail and then delete it. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help-- Alberto G. Murta Institute for Agriculture and Fisheries Research (INIAP-IPIMAR) Av. Brasilia, 1449-006 Lisboa, Portugal | Phone: +351 213027062 Fax:+351 213015948 | http://www.ipimar-iniap.ipimar.pt/pelagicos/