[R] Indexing from two variables

Ista Zahn izahn at psych.rochester.edu
Wed Feb 2 20:13:09 CET 2011


Hard to know exactly without seeing the structrure of rec and book,
but I would start with ?merge

Best,
Ista

On Wed, Feb 2, 2011 at 2:06 PM, dunner <ross.dunne at tcd.ie> wrote:
>
> Hello, thank you all for your patience and time
>
> I am essentially trying to get disorganised data into long form for linear
> modelling.
>
> I have 2 dataframes "rec" and "book"
>
> Each row in "book" needs to be pasted onto the end of several of the rows of
> "rec" according to two variables in the row:" MRN" and "COURSE" which match.
>
> I have tried the following and variations thereon to no avail:
>
> </code>
> #for each line of the recovery dataframe #insert the one line of the "book"
> dataset that corresponds to the MRN AND the course-of-treatment (COURSE)
>
> #get the mrn and course from the first line of the recovery dataframe (rec)
>
> i=1
> newlist=list()
> colnames(newlist)=colnames(book)
> for ( i in 1:dim(rec)[1]) {
>
>        mrn=as.numeric(as.vector(rec$MRN[i]));
>        course=as.character(rec$COURSE[i]);
>
> ## find the corresponding row in the book dataframe ## by generating a
> logical vector and using ## it to access "book"
>
> get.vector<-as.vector(((as.numeric(as.vector(book$MRN))==mrn) &
> (as.character(book$COURSE)==course)))
>
> #gives you a vector of logicals (works)
>
> newlist[i]<-book[get.vector,]  ### (doesn't work)
>
> i=i+1;
> }
> </code>
>
> If anyone has any suggestions on
>
> 1)getting this to work
> 2) making it more elegant (or perhaps just less clumsy)
>
> If I have been unclear in any way I beg your pardons.
>
> I do understand I haven't combined any data above, I think if I can generate
> a long-format dataframe I can combine them all on my own. Apologies to
> anybody who receives this twice, I had a "bounced" reply the first time, and
> assumed it was unsuccessful.
>
> Ross Dunne
> MB MRCPsych
> ross.dunne at tcd.ie
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Indexing-from-two-variables-tp3255405p3255405.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org



More information about the R-help mailing list