markleeds at verizon.net
2008-Dec-13 01:52 UTC
[R] odd behavior when multiplying data frame by an element
could someone explain what is happening below ? I was trying to solve a related question on the list and then , as I was solving it, I was getting strange answers and then I noticed below. It's obviously not a bug but I don't get it. Thanks. m <- data.frame(class = c("birds", "planes"), feather = c(1,3), jet = c(2,4)) d1 <- data.frame(jet = c(10), feather = c(20)) print(m) m[,-1]*2 # THIS IS FINE m[,"jet"]*2 # THIS IS FINE print(d1["jet"]) # THIS IS FINE m[,"jet"]*d1["jet"] # THIS IS ODD
markleeds at verizon.net
2008-Dec-13 02:06 UTC
[R] odd behavior when multiplying data frame by an element
disregard my last question. i forgot that d1 was a dataframe and was thinkiing it was a vector. i think i can fix what i was doing now. On Fri, Dec 12, 2008 at 8:52 PM, markleeds at verizon.net wrote:> could someone explain what is happening below ? I was trying to solve > a related question on the list and then , as I was solving it, > I was getting strange answers and then I noticed below. It's obviously > not a bug but I don't get it. Thanks. > > m <- data.frame(class = c("birds", "planes"), feather = c(1,3), jet = > c(2,4)) > d1 <- data.frame(jet = c(10), feather = c(20)) > > print(m) > m[,-1]*2 # THIS IS FINE > m[,"jet"]*2 # THIS IS FINE > > > print(d1["jet"]) # THIS IS FINE > m[,"jet"]*d1["jet"] # THIS IS ODD > > ______________________________________________ > R-help at 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.
Patrick Burns
2008-Dec-13 10:48 UTC
[Rd] [R] odd behavior when multiplying data frame by an element
I think it might be up for discussion whether or not there is a bug in here somewhere. > mat10 <- matrix(1:6, 3) > mat10 * 1:12 Error: dims [product 6] do not match the length of object [12] > data.frame(mat10) * 1:12 X1 X2 1 1 16 2 4 25 3 9 36 > version _ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status Under development (unstable) major 2 minor 9.0 year 2008 month 11 day 22 svn rev 47006 language R version.string R version 2.9.0 Under development (unstable) (2008-11-22 r47006) It seems dangerous to me that there is not at least a warning in the data frame case. I think I'd prefer an error like the matrix case. Patrick Burns patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User") markleeds at verizon.net wrote:> could someone explain what is happening below ? I was trying to solve > a related question on the list and then , as I was solving it, > I was getting strange answers and then I noticed below. It's obviously > not a bug but I don't get it. Thanks. > > m <- data.frame(class = c("birds", "planes"), feather = c(1,3), jet = > c(2,4)) > d1 <- data.frame(jet = c(10), feather = c(20)) > > print(m) > m[,-1]*2 # THIS IS FINE > m[,"jet"]*2 # THIS IS FINE > > > print(d1["jet"]) # THIS IS FINE > m[,"jet"]*d1["jet"] # THIS IS ODD > > ______________________________________________ > R-help at 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. > >
Possibly Parallel Threads
- odd behavior when multiplying data frame by an element
- How do I tapply to a data frame with arbitrary column labels?
- LLVM Cauldron 2016 (Sep 8th, Hebden Bridge, UK) registration and call for papers now open
- syslinux 2.11
- O'Reilly Open Source Convention in Portland