[R] Copying rows in data frames
arun
smartpink111 at yahoo.com
Tue Mar 19 14:30:07 CET 2013
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.
>>
>
More information about the R-help
mailing list