Hi all, I'm having some trouble with rbind - this may be a bug or it may be my misunderstanding. If I do fileName <- paste(tempdir(),"test.txt",sep="/") file.create(fileName) x <- read.table(fileName, col.names=c("one","two","three")) I get a data frame with no rows, as documented. If I then try to rbind this with another data frame with the same column names, I get an error: y <- data.frame(one=1,two=2,three=3) rbind(x,y) "Error in `*tmp*`[[jj]] : subscript out of bounds" On the other hand, doing "rbind(as.matrix(x),as.matrix(y))" works as expected. If this is, in fact, intended behaviour, could anyone suggest another way of achieving what I want, please? I'm trying to append to a data frame stored in a text file every so often, and of course I need to create it, as above, the first time around... My system is R-2.4.1/i686-pc-linux-gnu. Thanks in advance for your advice. Regards, Jon
Dear All, I have a very simple problem. I have a matrix called Predictors with headers X0...X24>dim(Predictors)[1] 79 25 Predictors X0 X1 X2 X3 X4 X5 X6 1 13.741200 12.148036 11.909435 11.671669 11.238207 10.864697 10.566351 2 10.240200 11.883900 11.529400 11.515050 11.411640 11.105800 10.813457 I would like to take each row and all the corresponding columns with out the header. I tried Predictors[1,1:25] but that retains the headers. Any suggestions. Thanks, Agnes
On Thu, 2007-01-25 at 15:22 +0000, Jon Clayden wrote:> Hi all, > > I'm having some trouble with rbind - this may be a bug or it may be my > misunderstanding. If I do > > fileName <- paste(tempdir(),"test.txt",sep="/") > file.create(fileName) > x <- read.table(fileName, col.names=c("one","two","three")) > > I get a data frame with no rows, as documented. If I then try to rbind > this with another data frame with the same column names, I get an > error: > > y <- data.frame(one=1,two=2,three=3) > rbind(x,y) > > "Error in `*tmp*`[[jj]] : subscript out of bounds" > > On the other hand, doing "rbind(as.matrix(x),as.matrix(y))" works as > expected. If this is, in fact, intended behaviour, could anyone > suggest another way of achieving what I want, please? I'm trying to > append to a data frame stored in a text file every so often, and of > course I need to create it, as above, the first time around... > > My system is R-2.4.1/i686-pc-linux-gnu. Thanks in advance for your advice. > > Regards, > JonThe problem is that the classes of the columns in 'x' are:> sapply(x, class)one two three "NULL" "NULL" "NULL" You might get more insight by reversing the two arguments:> rbind(y, x)Error in value[[jj]][ri] <- if (is.factor(xij)) as.vector(xij) else xij : incompatible types (from NULL to double) in subassignment type fix If you review the Details in ?rbind, you will note that there is a class checking component to the rbind'ing process for data frames. The lack of a class type for the columns in 'x' is the source of the problem. This is not part of rbind'ing matrices, since they can only be of a single data type. An alternative would be to use ifelse(): NewDF <- ifelse(nrow(x) == 0, y, rbind(x, y)) HTH, Marc Schwartz
On Thu, 2007-01-25 at 11:04 -0500, ablukacz wrote:> Dear All, > > I have a very simple problem. > I have a matrix called Predictors with headers X0...X24 > >dim(Predictors) > [1] 79 25 > > Predictors > X0 X1 X2 X3 X4 X5 X6 > 1 13.741200 12.148036 11.909435 11.671669 11.238207 10.864697 10.566351 > 2 10.240200 11.883900 11.529400 11.515050 11.411640 11.105800 10.813457 > > > > I would like to take each row and all the corresponding columns with out > the header. > I tried Predictors[1,1:25] but that retains the headers. > > > Any suggestions. > > Thanks, > > AgnesThe column names of X0, etc. suggest that Predictors is a data frame created by the use of one of the read.table() family of functions, not a matrix. An important difference depending upon what you intend to do with the data. However, in both cases, you can generally manipulate the data as if the headers were not present. In other words, most R functions will take the numeric values and act in a predictable (sorry for the pun) fashion. Think of the output above as if the column names X0 through X6 were A through G in a spreadsheet. They are simply labels, not part of the data itself. The same goes for the row names of 1 and 2. HTH, Marc Schwartz
Hi On 25 Jan 2007 at 10:44, Marc Schwartz wrote: From: Marc Schwartz <marc_schwartz at comcast.net> To: ablukacz <ablukacz at utm.utoronto.ca> Date sent: Thu, 25 Jan 2007 10:44:18 -0600 Copies to: r-help at stat.math.ethz.ch Subject: Re: [R] subscripting issues Send reply to: marc_schwartz at comcast.net <mailto:r-help-request at stat.math.ethz.ch?subject=unsubscribe> <mailto:r-help-request at stat.math.ethz.ch?subject=subscribe>> On Thu, 2007-01-25 at 11:04 -0500, ablukacz wrote: > > Dear All, > > > > I have a very simple problem. > > I have a matrix called Predictors with headers X0...X24 > > >dim(Predictors) > > [1] 79 25 > > > > Predictors > > X0 X1 X2 X3 X4 X5 > > X6 > > 1 13.741200 12.148036 11.909435 11.671669 11.238207 10.864697 > > 10.566351 2 10.240200 11.883900 11.529400 11.515050 11.411640 > > 11.105800 10.813457 > > > > > > > > I would like to take each row and all the corresponding columns with > > out the header. I tried Predictors[1,1:25] but that retains the > > headers. > > > > > > Any suggestions. > > > > Thanks, > > > > Agnes > > The column names of X0, etc. suggest that Predictors is a data frame > created by the use of one of the read.table() family of functions, notalthough you are probably right, it could be matrix as well> is.data.frame(zeta)[1] TRUE> is.matrix(zeta)[1] FALSE> > zetam<-as.matrix(zeta) > zetamtepl tio2 al2o3 iep 1 60 1 3.5 5.65 2 60 1 2.0 5.00 3 60 0 3.5 5.30 4 60 0 2.0 4.65 5 40 1 3.5 5.20 6 40 1 2.0 4.85 7 40 0 3.5 5.70 8 40 0 2.0 5.25> is.matrix(zetam)[1] TRUE> is.data.frame(zetam)[1] FALSE Cheers Petr> a matrix. An important difference depending upon what you intend to do > with the data. > > However, in both cases, you can generally manipulate the data as if > the headers were not present. In other words, most R functions will > take the numeric values and act in a predictable (sorry for the pun) > fashion. > > Think of the output above as if the column names X0 through X6 were A > through G in a spreadsheet. They are simply labels, not part of the > data itself. The same goes for the row names of 1 and 2. > > HTH, > > Marc Schwartz > > ______________________________________________ > R-help at stat.math.ethz.ch 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.Petr Pikal petr.pikal at precheza.cz
Possibly Parallel Threads
- rbind has confusing result for custom sub-class (possible bug?)
- rbind has confusing result for custom sub-class (possible bug?)
- rbind has confusing result for custom sub-class (possible bug?)
- rbind has confusing result for custom sub-class (possible bug?)
- R loop help