Ross Culloch
2009-Jul-30 18:46 UTC
[R] edit.row.names taking row names from the edited dataframe
Hi all, I am struggling to work out how to use the rownames from an edited dataframe rather than the row names from the original dataframe. In my data set i'm trying to extract several rows of data on particular individuals, i don't doubt i'm using the long way round but what i have in the way of a script is this: ##selecting the IDs from the dataframe individually A1<-mumpup[ID=="A1",] B1<-mumpup[ID=="B1",] B2<-mumpup[ID=="B2",] B3<-mumpup[ID=="B3",] B4<-mumpup[ID=="B4",] B6<-mumpup[ID=="B6",] B7<-mumpup[ID=="B7",] B8<-mumpup[ID=="B8",] B9<-mumpup[ID=="B9",] B13<-mumpup[ID=="B13",] C1<-mumpup[ID=="C1",] G1<-mumpup[ID=="G1",] data<-rbind(A1,B1,B2,B3,B4,B6,B7,B8,B9,B13,C1,G1) It works fine to a certain extent, the only problem being that i get are all the IDs in the original dataframe so if i use summary(data$ID) I get: A1 B1 B10 B13 B2 B3 B4 B6 B7 B8 B9 C1 G1 G3 H2 H9 J1 J2 J3 K1 354 354 0 354 354 354 354 354 354 354 354 246 210 0 0 0 0 0 0 0 So it does take the data out of the dataframe but it keeps the IDs for some reason. I have looked at the edit.data.frame help and i understand why it is happening, it is taking the rownames from the original dataframe and not the edit and it seems i should use edit.row.names=T in my script, but i can't get that to work. Does anyone have any suggestions at all? Any help is much appreciated, Best wishes, Ross -- View this message in context: http://www.nabble.com/edit.row.names-taking-row-names-from-the-edited-dataframe-tp24744741p24744741.html Sent from the R help mailing list archive at Nabble.com.
Erik Iverson
2009-Jul-30 18:55 UTC
[R] edit.row.names taking row names from the edited dataframe
Hello, Try the following instead of your many separate assignments (I do not like to give objects the name "data", since that is the name of a built-in function).> ids <- subset(mumpup, id %in% c("A1", "B1", ... , "G1"))#in above line, fill in ... with other IDs you want> ids$ID <- factor(ids$ID)#remove extra factor levels, this would also work after the rbind #if you called your object "ids" instead of "data". This really has nothing to do with the "row names" issue you mention, as far as I can tell. It has to do with this: http://stackoverflow.com/questions/1195826/dropping-factor-levels-in-a-subsetted-data-frame-in-r Best, Erik Iverson -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Ross Culloch Sent: Thursday, July 30, 2009 1:46 PM To: r-help at r-project.org Subject: [R] edit.row.names taking row names from the edited dataframe Hi all, I am struggling to work out how to use the rownames from an edited dataframe rather than the row names from the original dataframe. In my data set i'm trying to extract several rows of data on particular individuals, i don't doubt i'm using the long way round but what i have in the way of a script is this: ##selecting the IDs from the dataframe individually A1<-mumpup[ID=="A1",] B1<-mumpup[ID=="B1",] B2<-mumpup[ID=="B2",] B3<-mumpup[ID=="B3",] B4<-mumpup[ID=="B4",] B6<-mumpup[ID=="B6",] B7<-mumpup[ID=="B7",] B8<-mumpup[ID=="B8",] B9<-mumpup[ID=="B9",] B13<-mumpup[ID=="B13",] C1<-mumpup[ID=="C1",] G1<-mumpup[ID=="G1",] data<-rbind(A1,B1,B2,B3,B4,B6,B7,B8,B9,B13,C1,G1) It works fine to a certain extent, the only problem being that i get are all the IDs in the original dataframe so if i use summary(data$ID) I get: A1 B1 B10 B13 B2 B3 B4 B6 B7 B8 B9 C1 G1 G3 H2 H9 J1 J2 J3 K1 354 354 0 354 354 354 354 354 354 354 354 246 210 0 0 0 0 0 0 0 So it does take the data out of the dataframe but it keeps the IDs for some reason. I have looked at the edit.data.frame help and i understand why it is happening, it is taking the rownames from the original dataframe and not the edit and it seems i should use edit.row.names=T in my script, but i can't get that to work. Does anyone have any suggestions at all? Any help is much appreciated, Best wishes, Ross -- View this message in context: http://www.nabble.com/edit.row.names-taking-row-names-from-the-edited-dataframe-tp24744741p24744741.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.
Chuck Cleland
2009-Jul-30 18:57 UTC
[R] edit.row.names taking row names from the edited dataframe
On 7/30/2009 2:46 PM, Ross Culloch wrote:> Hi all, > > I am struggling to work out how to use the rownames from an edited dataframe > rather than the row names from the original dataframe. In my data set i'm > trying to extract several rows of data on particular individuals, i don't > doubt i'm using the long way round but what i have in the way of a script is > this: > > > ##selecting the IDs from the dataframe individually > A1<-mumpup[ID=="A1",] > B1<-mumpup[ID=="B1",] > B2<-mumpup[ID=="B2",] > B3<-mumpup[ID=="B3",] > B4<-mumpup[ID=="B4",] > B6<-mumpup[ID=="B6",] > B7<-mumpup[ID=="B7",] > B8<-mumpup[ID=="B8",] > B9<-mumpup[ID=="B9",] > B13<-mumpup[ID=="B13",] > C1<-mumpup[ID=="C1",] > G1<-mumpup[ID=="G1",] > > data<-rbind(A1,B1,B2,B3,B4,B6,B7,B8,B9,B13,C1,G1) > > It works fine to a certain extent, the only problem being that i get are all > the IDs in the original dataframe so if i use > > summary(data$ID) > > I get: > > A1 B1 B10 B13 B2 B3 B4 B6 B7 B8 B9 C1 G1 G3 H2 H9 J1 J2 J3 > K1 > 354 354 0 354 354 354 354 354 354 354 354 246 210 0 0 0 0 0 0 > 0 > > So it does take the data out of the dataframe but it keeps the IDs for some > reason. > > I have looked at the edit.data.frame help and i understand why it is > happening, it is taking the rownames from the original dataframe and not the > edit and it seems i should use edit.row.names=T in my script, but i can't > get that to work. > > Does anyone have any suggestions at all? Any help is much appreciated,If I understand, you may want something like this: myIDs <- c('A1','B1','B2','B3','B4','B6','B7','B8','B9','B13','C1','G1') subset(mumpup, ID %in% myIDs)> Best wishes, > > Ross-- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
Ross Culloch
2009-Jul-30 19:17 UTC
[R] edit.row.names taking row names from the edited dataframe
Hello Erik, Thanks very much, that works perfectly - thanks also for the link - it seems that i was going down the wrong road with regards to solving this problem! Thanks also for your quick reply! -- View this message in context: http://www.nabble.com/edit.row.names-taking-row-names-from-the-edited-dataframe-tp24744741p24745624.html Sent from the R help mailing list archive at Nabble.com.
Ottorino-Luca Pantani
2009-Aug-19 15:36 UTC
[R] edit.row.names taking row names from the edited dataframe
Erik Iverson ha scritto:> > This really has nothing to do with the "row names" issue you mention, as far as I can tell. It has to do with this: > > http://stackoverflow.com/questions/1195826/dropping-factor-levels-in-a-subsetted-data-frame-in-r > > Best, > Erik Iverson >Perhaps you may also take a look at this http://wiki.r-project.org/rwiki/doku.php?id=tips:data-manip:drop_unused_levels df.mydata$problemFactor <- df.mydata$problemFactor[, drop = TRUE] 8rino