seems like a transpose, so use
?t
t(your.data.frame)
On 22 October 2014 11:34, Matthias Weber wrote:
> Hello together,
>
> i have a little problem. Maybe anyone can help me.
>
> I have a data. frame which look like this one:
> 1000 1001 10021003
> 15 6 12
rame
>>> dd <- data.frame(b = c("chr2", "chr1", "chrY", "chr13", "chrX"),
>>> x = c("A", "D", "A", "C", "C"), y = c(8, 3, 9, 9,7),
>>> z = c(1, 1, 1, 2, 8)
he column b consists of not only number but also one letter after
"chr",
for example chrX, chrY. I want to put them after number but in the
order of
ASCII.
i.e. chr1--- On *Thu, 20/5/10, Jorge Ivan Velez *
wrote:
From: Jorge Ivan Velez
Subject: Re: [R] sort a data.frame
To: &quo
it's a excellent solution. I am sorry I missed something in my question.
the column b consists of not only number but also one letter after "chr",
for example chrX, chrY. I want to put them after number but in the order of
ASCII.
i.e. chr1 wrote:
From: Jorge Ivan Velez
Subject:
ot;),
> x = c("A", "D", "A", "C", "C"), y = c(8, 3, 9, 9,7),
>z = c(1, 1, 1, 2, 8))
>
> the expected result
>
>b x y z
> 1 chr1 D 3 1
> 2 chr2 A 8 1
> 3 chr13 C 9 2
> 4 chrX C 7 8
> 5 chrY
If you want to sort the data frame according to column "b", the
followding code does this work.
attach(dd)
dd<-dd[order(b),]
detach(dd)
If you want to sort the data frame according to the chr number in column
b, you should extract the numbers first into a vector, say chrnum, and
then use order
Hi Yuan,
Try
dd[order(as.numeric(gsub("[^0-9]", "", dd$b))), ]
HTH,
Jorge
On Thu, May 20, 2010 at 8:28 AM, Yuan Jian <> wrote:
> Hello,
>
> I have a dataframe:
> dd <- data.frame(b = c("chr2", "chr1", "chr15", "chr13"),
> x = c("A", "D", "A", "C"), y = c(8, 3, 9, 9),
>z = c(1, 1,
The solution given by Jim is more correct
dd[order(as.numeric(substr(dd$b,4,5))),]
regards
M
jim holtman a écrit :
dd
b x y z
1 chr2 A 8 1
2 chr1 D 3 1
3 chr15 A 9 1
4 chr13 C 9 2
# add column with just numbers
dd$sort <- as.integer(gsub("\\D+", "", dd$b))
dd[order(dd$sort),]
Try this.
dd[order(gsub("chr","",dd$b)),]
You need regular expressions if chr is not the only characterstring
that is prepended to the numbers.
look for
?strsplit
Nikhil Kaza
University of North Carolina
nikhil.l...@gmail.com
On May 20, 2010, at 8:28 AM, Yuan Jian wrote:
Hello,
I have
> dd
b x y z
1 chr2 A 8 1
2 chr1 D 3 1
3 chr15 A 9 1
4 chr13 C 9 2
> # add column with just numbers
> dd$sort <- as.integer(gsub("\\D+", "", dd$b))
> dd[order(dd$sort),] # notice it is a numeric, not character order
b x y z sort
2 chr1 D 3 11
1 chr2 A 8 12
4 chr13 C 9 2 1
10 matches
Mail list logo