[R] Merging and subsetting with row names XXXX

Petr PIKAL petr.pikal at precheza.cz
Wed Jan 4 17:03:56 CET 2012


Hi

As you did not provide any data you probably can not get canned solution

> Hello everyone,
> 
> I have two questions:
> 
> 1)
> 
> I want to create a subset of a data frame column-wise and simultaneously
> extract the row names into a "proper" variable. I tried this, but 
received
> an error:
> 
> > myleft<-mydata[c(id=row.names(mydata),"workshop","gender","q1","q2")]
> Error in `[.data.frame`(mydata, c(id = row.names(mydata), "workshop",  :
>   undefined columns selected
> Is the following the best way to go about this:
> 
> >
> myleft<-data.frame(id=row.names(mydata),mydata)[c
> ("id","workshop","gender","q1","q2")]
> > myleft

If I understand correctly you want only some columns, all rows and make 
also new column identical with row names.

either
cbind(id=row.names(mydata),mydata[, c(2,4,5,6)])

or
data.frame(id=row.names(mydata),mydata[, c(2,4,5,6)])
 
can do it.

> 
> 2)
> 
> Is it possible to merge data using merge() with a by variable that is 
the

AFAI no.

> row names? I tried this:
> 
> > both<-merge(myleft,myright,by=row.names(myleft))
> Error in fix.by(by.x, x) : 'by' must specify valid column(s)
> > both
> Suggestions?

Regards
Petr


> 
> 
> Thanks!
> 
> Dan
> 
>    [[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.



More information about the R-help mailing list