Hi, I'm trying to copy the first row of one data frame to another. This is the statement I am using : df2[1,]<-df1[1,]; I have printed them out separately: df1[1,] = A C D E F But after copying: df2[1,] = 96 29 88 122 68 Why isn't it copying? They are both data frames, and "as.character" isn't working either. Thanks for your input :) [[alternative HTML version deleted]]
show us the data. On Tue, Mar 19, 2013 at 7:55 AM, Sahana Srinivasan <sahanasrinivasan.91 at gmail.com> wrote:> Hi, > I'm trying to copy the first row of one data frame to another. This is the > statement I am using : > > df2[1,]<-df1[1,]; > > I have printed them out separately: > df1[1,] = A C D E F > But after copying: > df2[1,] = 96 29 88 122 68 > > > Why isn't it copying? They are both data frames, and "as.character" isn't > working either. > > Thanks for your input :) > > [[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.
Hi, set.seed(25) ?df1<- as.data.frame(matrix(sample(LETTERS[1:10],20,replace=TRUE),ncol=5),stringsAsFactors=FALSE) ?df1 #? V1 V2 V3 V4 V5 #1? E? B? A? J? F #2? G? J? C? F? H #3? B? G? D? G? E #4? I? D? D? B? H ?str(df1) #'data.frame':??? 4 obs. of? 5 variables: # $ V1: chr? "E" "G" "B" "I" # $ V2: chr? "B" "J" "G" "D" # $ V3: chr? "A" "C" "D" "D" # $ V4: chr? "J" "F" "G" "B" # $ V5: chr? "F" "H" "E" "H" ?df2<-as.data.frame(matrix(NA,ncol=5,nrow=3)) ?df2[1,]<-df1[1,] ?df2[1,] #? V1 V2 V3 V4 V5 #1? E? B? A? J? F A.K. ----- Original Message ----- From: Sahana Srinivasan <sahanasrinivasan.91 at gmail.com> To: r-help at r-project.org Cc: Sent: Tuesday, March 19, 2013 7:55 AM Subject: [R] Copying rows in data frames Hi, I'm trying to copy the first row of one data frame to another. This is the statement I am using : df2[1,]<-df1[1,]; I have printed them out separately: df1[1,] = A C D E F But after copying: df2[1,] = 96 29 88 122 68 Why isn't it copying? They are both data frames, and "as.character" isn't working either. Thanks for your input :) ??? [[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.
Hi, In your original message, you were trying to copy the colnames of df1.? I am not sure that is what you wanted.? Also, there were some spaces between the columns, which I had to delete. This is how your dataset looks like now: dat1<-read.csv("dataSahana1.csv",sep="\t",header=TRUE,stringsAsFactors=FALSE) str(dat1) #'data.frame':??? 4625 obs. of? 21 variables: # $ GENE: chr? "amt:Amet_0001" "amt:Amet_0002" "amt:Amet_0003" "amt:Amet_0004" ... # $ A?? : int? 29 19 1 13 7 42 44 4 6 13 ... # $ C?? : int? 0 3 0 0 1 2 2 1 3 1 ... # $ D?? : int? 23 15 4 18 2 30 60 5 13 16 ... # $ E?? : int? 34 42 6 29 9 65 77 12 11 30 ... # $ F?? : int? 17 12 1 10 5 19 16 4 5 12 ... # $ G?? : int? 16 18 9 22 4 59 58 7 10 7 ... # $ H?? : int? 12 8 0 8 4 11 17 0 1 4 ... # $ I?? : int? 42 35 9 32 12 53 88 14 20 21 ... # $ K?? : int? 29 25 10 38 11 44 71 13 16 28 ... # $ L?? : int? 39 43 9 50 8 51 75 13 6 24 ... # $ M?? : int? 6 7 1 6 1 16 27 3 5 12 ... # $ N?? : int? 35 26 3 26 2 27 40 5 9 19 ... # $ P?? : int? 20 13 1 8 4 16 22 1 4 4 ... # $ Q?? : int? 13 9 3 21 2 24 21 4 1 10 ... # $ R?? : int? 25 14 2 19 4 36 49 2 2 17 ... # $ S?? : int? 34 21 1 12 9 28 37 4 7 14 ... # $ T?? : int? 27 20 0 12 4 41 43 5 10 11 ... # $ V?? : int? 32 30 8 28 4 42 60 5 10 13 ... # $ W?? : int? 3 0 0 1 0 1 0 1 0 1 ... # $ Y?? : int? 12 8 1 15 2 28 23 2 2 9 ... ?dat1[1:3,] #?????????? GENE? A C? D? E? F? G? H? I? K? L M? N? P? Q? R? S? T? V W? Y #1 amt:Amet_0001 29 0 23 34 17 16 12 42 29 39 6 35 20 13 25 34 27 32 3 12 #2 amt:Amet_0002 19 3 15 42 12 18? 8 35 25 43 7 26 13? 9 14 21 20 30 0? 8 #3 amt:Amet_0003? 1 0? 4? 6? 1? 9? 0? 9 10? 9 1? 3? 1? 3? 2? 1? 0? 8 0? 1 ?dat2<- data.frame(matrix(NA,ncol=20,nrow=5)) ?dat2[1,]<-dat1[1,-1] dat2[1,] #? X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 X16 X17 X18 X19 X20 #1 29? 0 23 34 17 16 12 42 29? 39?? 6? 35? 20? 13? 25? 34? 27? 32?? 3? 12 #If you wanted the colnames of dat1 dat2[1,]<- colnames(dat1)[-1] ?dat2[1,] #? X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 X16 X17 X18 X19 X20 #1? A? C? D? E? F? G? H? I? K?? L?? M?? N?? P?? Q?? R?? S?? T?? V?? W?? Y A.K. ________________________________ From: Sahana Srinivasan <sahanasrinivasan.91 at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Tuesday, March 19, 2013 8:42 AM Subject: Re: [R] Copying rows in data frames This is the file I am reading in. The dput() command keeps getting truncated, and I thought it might be easier to go straight to the bottom of this. Sorry about being so unwieldy, R is new to m and a lot more confusing than I thought. On Tue, Mar 19, 2013 at 12:37 PM, arun <smartpink111 at yahoo.com> wrote: Could you subset your whole dataset to 10 rows and 10 columns etc....?>dput(df2[1:10,1:10]) > > > > > > > >________________________________ >From: Sahana Srinivasan <sahanasrinivasan.91 at gmail.com> >To: arun <smartpink111 at yahoo.com> >Sent: Tuesday, March 19, 2013 8:32 AM > >Subject: Re: [R] Copying rows in data frames > > > >The whole thing does not show when I print it on the console. It's essentially "amt : Amet_0001" till 4802. >
Just by reading your dataset (without any modifications) datNew<- read.table("data.seq.ptseq.rescount.txt",sep="\t") str(datNew) 'data.frame':??? 4625 obs. of? 22 variables: ?$ GENE: logi? NA NA NA NA NA NA ... ?$ X?? : int? 29 19 1 13 7 42 44 4 6 13 ... ?$ A?? : int? 0 3 0 0 1 2 2 1 3 1 ... ?$ C?? : int? 23 15 4 18 2 30 60 5 13 16 ... ?$ D?? : int? 34 42 6 29 9 65 77 12 11 30 ... ?$ E?? : int? 17 12 1 10 5 19 16 4 5 12 ... ?$ F?? : int? 16 18 9 22 4 59 58 7 10 7 ... ?$ G?? : int? 12 8 0 8 4 11 17 0 1 4 ... ?$ H?? : int? 42 35 9 32 12 53 88 14 20 21 ... ?$ I?? : int? 29 25 10 38 11 44 71 13 16 28 ... ?$ K?? : int? 39 43 9 50 8 51 75 13 6 24 ... ?$ L?? : int? 6 7 1 6 1 16 27 3 5 12 ... ?$ M?? : int? 35 26 3 26 2 27 40 5 9 19 ... ?$ N?? : int? 20 13 1 8 4 16 22 1 4 4 ... ?$ P?? : int? 13 9 3 21 2 24 21 4 1 10 ... ?$ Q?? : int? 25 14 2 19 4 36 49 2 2 17 ... ?$ R?? : int? 34 21 1 12 9 28 37 4 7 14 ... ?$ S?? : int? 27 20 0 12 4 41 43 5 10 11 ... ?$ T?? : int? 32 30 8 28 4 42 60 5 10 13 ... ?$ V?? : int? 3 0 0 1 0 1 0 1 0 1 ... ?$ W?? : int? 12 8 1 15 2 28 23 2 2 9 ... ?$ Y?? : logi? NA NA NA NA NA NA ... ?datNew[1:3,] #????????????? GENE? X A? C? D? E? F? G? H? I? K L? M? N? P? Q? R? S? T V? W? Y #amt:Amet_0001?? NA 29 0 23 34 17 16 12 42 29 39 6 35 20 13 25 34 27 32 3 12 NA #amt:Amet_0002?? NA 19 3 15 42 12 18? 8 35 25 43 7 26 13? 9 14 21 20 30 0? 8 NA #amt:Amet_0003?? NA? 1 0? 4? 6? 1? 9? 0? 9 10? 9 1? 3? 1? 3? 2? 1? 0? 8 0? 1 NA #Here, the GENE column is all "NA" and also the last column Y.? It is due to some spaces. dim(datNew) #[1] 4625?? 22 #But, it still gives me what I wanted ?dat2[1,]<- colnames(datNew)[-c(1:2)] ?dat2[1,] #? X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 X16 X17 X18 X19 X20 #1? A? C? D? E? F? G? H? I? K?? L?? M?? N?? P?? Q?? R?? S?? T?? V?? W?? Y A.K. ________________________________ From: Sahana Srinivasan <sahanasrinivasan.91 at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Tuesday, March 19, 2013 9:15 AM Subject: Re: [R] Copying rows in data frames Yes, I did want to copy the column names from the first data frame to the second, but the characters were being replaced with random numbers. Is there any logic to the numbers that replaced A, C, D, E.. etc? On Tue, Mar 19, 2013 at 1:10 PM, arun <smartpink111 at yahoo.com> wrote:> >Hi, >In your original message, you were trying to copy the colnames of df1.? I am not sure that is what you wanted.? Also, there were some spaces between the columns, which I had to delete. > >This is how your dataset looks like now: > >dat1<-read.csv("dataSahana1.csv",sep="\t",header=TRUE,stringsAsFactors=FALSE) >str(dat1) >#'data.frame':??? 4625 obs. of? 21 variables: ># $ GENE: chr? "amt:Amet_0001" "amt:Amet_0002" "amt:Amet_0003" "amt:Amet_0004" ... ># $ A?? : int? 29 19 1 13 7 42 44 4 6 13 ... ># $ C?? : int? 0 3 0 0 1 2 2 1 3 1 ... ># $ D?? : int? 23 15 4 18 2 30 60 5 13 16 ... ># $ E?? : int? 34 42 6 29 9 65 77 12 11 30 ... ># $ F?? : int? 17 12 1 10 5 19 16 4 5 12 ... ># $ G?? : int? 16 18 9 22 4 59 58 7 10 7 ... ># $ H?? : int? 12 8 0 8 4 11 17 0 1 4 ... ># $ I?? : int? 42 35 9 32 12 53 88 14 20 21 ... ># $ K?? : int? 29 25 10 38 11 44 71 13 16 28 ... ># $ L?? : int? 39 43 9 50 8 51 75 13 6 24 ... ># $ M?? : int? 6 7 1 6 1 16 27 3 5 12 ... ># $ N?? : int? 35 26 3 26 2 27 40 5 9 19 ... ># $ P?? : int? 20 13 1 8 4 16 22 1 4 4 ... ># $ Q?? : int? 13 9 3 21 2 24 21 4 1 10 ... ># $ R?? : int? 25 14 2 19 4 36 49 2 2 17 ... ># $ S?? : int? 34 21 1 12 9 28 37 4 7 14 ... ># $ T?? : int? 27 20 0 12 4 41 43 5 10 11 ... ># $ V?? : int? 32 30 8 28 4 42 60 5 10 13 ... ># $ W?? : int? 3 0 0 1 0 1 0 1 0 1 ... ># $ Y?? : int? 12 8 1 15 2 28 23 2 2 9 ... >?dat1[1:3,] >#?????????? GENE? A C? D? E? F? G? H? I? K? L M? N? P? Q? R? S? T? V W? Y >#1 amt:Amet_0001 29 0 23 34 17 16 12 42 29 39 6 35 20 13 25 34 27 32 3 12 >#2 amt:Amet_0002 19 3 15 42 12 18? 8 35 25 43 7 26 13? 9 14 21 20 30 0? 8 >#3 amt:Amet_0003? 1 0? 4? 6? 1? 9? 0? 9 10? 9 1? 3? 1? 3? 2? 1? 0? 8 0? 1 >?dat2<- data.frame(matrix(NA,ncol=20,nrow=5)) >?dat2[1,]<-dat1[1,-1] >dat2[1,] >#? X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 X16 X17 X18 X19 X20 >#1 29? 0 23 34 17 16 12 42 29? 39?? 6? 35? 20? 13? 25? 34? 27? 32?? 3? 12 >#If you wanted the colnames of dat1 >dat2[1,]<- colnames(dat1)[-1] >?dat2[1,] >#? X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 X16 X17 X18 X19 X20 >#1? A? C? D? E? F? G? H? I? K?? L?? M?? N?? P?? Q?? R?? S?? T?? V?? W?? Y > >A.K. > > > > > >________________________________ >From: Sahana Srinivasan <sahanasrinivasan.91 at gmail.com> >To: arun <smartpink111 at yahoo.com> >Sent: Tuesday, March 19, 2013 8:42 AM > >Subject: Re: [R] Copying rows in data frames > > >This is the file I am reading in. The dput() command keeps getting truncated, and I thought it might be easier to go straight to the bottom of this. > > >Sorry about being so unwieldy, R is new to m and a lot more confusing than I thought. > > > > >On Tue, Mar 19, 2013 at 12:37 PM, arun <smartpink111 at yahoo.com> wrote: > >Could you subset your whole dataset to 10 rows and 10 columns etc....? >>dput(df2[1:10,1:10]) >> >> >> >> >> >> >> >>________________________________ >>From: Sahana Srinivasan <sahanasrinivasan.91 at gmail.com> >>To: arun <smartpink111 at yahoo.com> >>Sent: Tuesday, March 19, 2013 8:32 AM >> >>Subject: Re: [R] Copying rows in data frames >> >> >> >>The whole thing does not show when I print it on the console. It's essentially "amt : Amet_0001" till 4802. >> >