[R] sort a data.frame in a different way
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 1211 24 3 81 What i need is a data.frame, which looks like this one. The Column names should be in the first column, and the values right of the column name. The solution look like this one: A B 1000 5 4 1001 6 3 1002 12 8 1003 11 1 maybe anyone can help me. Thank you. Best regards. Mat This e-mail may contain trade secrets, privileged, undisclosed or otherwise confidential information. If you have received this e-mail in error, you are hereby notified that any review, copying or distribution of it is strictly prohibited. Please inform us immediately and destroy the original transmittal. Thank you for your cooperation. Diese E-Mail kann Betriebs- oder Geschaeftsgeheimnisse oder sonstige vertrauliche Informationen enthalten. Sollten Sie diese E-Mail irrtuemlich erhalten haben, ist Ihnen eine Kenntnisnahme des Inhalts, eine Vervielfaeltigung oder Weitergabe der E-Mail ausdruecklich untersagt. Bitte benachrichtigen Sie uns und vernichten Sie die empfangene E-Mail. Vielen Dank. [[alternative HTML version deleted]] __ R-help@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.
Re: [R] sort a data.frame in a different way
seems like a transpose, so use ?t t(your.data.frame) On 22 October 2014 11:34, Matthias Weber matthias.we...@fntsoftware.com 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 1211 24 3 81 What i need is a data.frame, which looks like this one. The Column names should be in the first column, and the values right of the column name. The solution look like this one: A B 1000 5 4 1001 6 3 1002 12 8 1003 11 1 maybe anyone can help me. Thank you. Best regards. Mat This e-mail may contain trade secrets, privileged, undisclosed or otherwise confidential information. If you have received this e-mail in error, you are hereby notified that any review, copying or distribution of it is strictly prohibited. Please inform us immediately and destroy the original transmittal. Thank you for your cooperation. Diese E-Mail kann Betriebs- oder Geschaeftsgeheimnisse oder sonstige vertrauliche Informationen enthalten. Sollten Sie diese E-Mail irrtuemlich erhalten haben, ist Ihnen eine Kenntnisnahme des Inhalts, eine Vervielfaeltigung oder Weitergabe der E-Mail ausdruecklich untersagt. Bitte benachrichtigen Sie uns und vernichten Sie die empfangene E-Mail. Vielen Dank. [[alternative HTML version deleted]] please read mailing list guidelines, and don't send HTML messages __ R-help@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. __ R-help@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.
Re: [R] sort a data.frame
But you should both be warned that would only be correct for a species where there were 24 chromosomes. This would work for humans and species with a higher number: dd[ order(as.numeric(substring(dd$b, 4), substring(dd$b, 4))), ] -- David Winsemius, MD West Hartford, CT On May 20, 2010, at 10:24 PM, Jorge Ivan Velez wrote: Hi Yuan, One way would be: dd[order(factor(substring(dd$b, 4), levels = c(1:22, LETTERS[1:25]))),] HTH, Jorge On Thu, May 20, 2010 at 10:18 PM, Yuan Jian wrote: 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. chr1chr2chr9chr10...chr22chrA...chrXchrY if I have dataframe 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)) 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 A 9 1 --- On *Thu, 20/5/10, Jorge Ivan Velez jorgeivanve...@gmail.com* wrote: From: Jorge Ivan Velez jorgeivanve...@gmail.com Subject: Re: [R] sort a data.frame To: Yuan Jian jayuan2...@yahoo.com Cc: r-help@r-project.org Received: Thursday, 20 May, 2010, 1:31 PM 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, 1, 2)) 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 Now I want to sort them according column b, but only its number is considered: b x y z 1 chr1 D 3 1 2 chr13 C 9 2 3 chr15 A 9 1 4 chr2 A 8 1 thanks jian __ R-help@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.
Re: [R] sort a data.frame
Also see mixedsort and mixedorder in the gtools package. 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, 1, 2)) dd[gtools::mixedorder(dd$b),] R b x y z 2 chr1 D 3 1 1 chr2 A 8 1 4 chr13 C 9 2 3 chr15 A 9 1 Kevin Wright On Fri, May 21, 2010 at 7:56 AM, David Winsemius dwinsem...@comcast.net wrote: But you should both be warned that would only be correct for a species where there were 24 chromosomes. This would work for humans and species with a higher number: dd[ order(as.numeric(substring(dd$b, 4), substring(dd$b, 4))), ] -- David Winsemius, MD West Hartford, CT On May 20, 2010, at 10:24 PM, Jorge Ivan Velez wrote: Hi Yuan, One way would be: dd[order(factor(substring(dd$b, 4), levels = c(1:22, LETTERS[1:25]))),] HTH, Jorge On Thu, May 20, 2010 at 10:18 PM, Yuan Jian wrote: 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. chr1chr2chr9chr10...chr22chrA...chrXchrY if I have dataframe 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)) 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 A 9 1 --- On *Thu, 20/5/10, Jorge Ivan Velez jorgeivanve...@gmail.com* wrote: From: Jorge Ivan Velez jorgeivanve...@gmail.com Subject: Re: [R] sort a data.frame To: Yuan Jian jayuan2...@yahoo.com Cc: r-help@r-project.org Received: Thursday, 20 May, 2010, 1:31 PM 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, 1, 2)) 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 Now I want to sort them according column b, but only its number is considered: b x y z 1 chr1 D 3 1 2 chr13 C 9 2 3 chr15 A 9 1 4 chr2 A 8 1 thanks jian __ R-help@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. -- Kevin Wright __ R-help@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.
[R] sort a data.frame
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, 1, 2)) 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 Now I want to sort them according column b, but only its number is considered: b x y z 1 chr1 D 3 1 2 chr13 C 9 2 3 chr15 A 9 1 4 chr2 A 8 1 thanks jian [[alternative HTML version deleted]] __ R-help@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.
Re: [R] sort a data.frame
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 13 3 chr15 A 9 1 15 On Thu, May 20, 2010 at 8:28 AM, Yuan Jian jayuan2...@yahoo.com 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, 1, 2)) 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 Now I want to sort them according column b, but only its number is considered: b x y z 1 chr1 D 3 1 2 chr13 C 9 2 3 chr15 A 9 1 4 chr2 A 8 1 thanks jian [[alternative HTML version deleted]] __ R-help@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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? __ R-help@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.
Re: [R] sort a data.frame
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 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, 1, 2)) 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 Now I want to sort them according column b, but only its number is considered: b x y z 1 chr1 D 3 1 2 chr13 C 9 2 3 chr15 A 9 1 4 chr2 A 8 1 thanks jian [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.
Re: [R] sort a data.frame
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),] # 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 13 3 chr15 A 9 1 15 On Thu, May 20, 2010 at 8:28 AM, Yuan Jian jayuan2...@yahoo.com 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, 1, 2)) 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 Now I want to sort them according column b, but only its number is considered: b x y z 1 chr1 D 3 1 2 chr13 C 9 2 3 chr15 A 9 1 4 chr2 A 8 1 thanks jian [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.
Re: [R] sort a data.frame
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, 1, 2)) 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 Now I want to sort them according column b, but only its number is considered: b x y z 1 chr1 D 3 1 2 chr13 C 9 2 3 chr15 A 9 1 4 chr2 A 8 1 thanks jian [[alternative HTML version deleted]] __ R-help@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. [[alternative HTML version deleted]] __ R-help@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.
Re: [R] sort a data.frame
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(chrnum) as the index to sort the data frame dd. Suppose that the chr numbers have been extracted to vector chrnum. The code is as follows: chrnum -c(2,1,15,13) attach(dd) dd-dd[order(chrnum),] detach(dd) The result is dd b x y z 2 chr1 D 3 1 1 chr2 A 8 1 4 chr13 C 9 2 3 chr15 A 9 1 But I don't know how to extract the numbers conveniently. Please respond if anyone knows. Many thanks! On 05/20/2010 08:28 PM, 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, 1, 2)) 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 Now I want to sort them according column b, but only its number is considered: b x y z 1 chr1 D 3 1 2 chr13 C 9 2 3 chr15 A 9 1 4 chr2 A 8 1 thanks jian __ R-help@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.
Re: [R] sort a data.frame
Hi Yuan, One way would be: dd[order(factor(substring(dd$b, 4), levels = c(1:22, LETTERS[1:25]))),] HTH, Jorge On Thu, May 20, 2010 at 10:18 PM, Yuan Jian wrote: 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. chr1chr2chr9chr10...chr22chrA...chrXchrY if I have dataframe 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)) 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 A 9 1 --- On *Thu, 20/5/10, Jorge Ivan Velez jorgeivanve...@gmail.com* wrote: From: Jorge Ivan Velez jorgeivanve...@gmail.com Subject: Re: [R] sort a data.frame To: Yuan Jian jayuan2...@yahoo.com Cc: r-help@r-project.org Received: Thursday, 20 May, 2010, 1:31 PM 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, 1, 2)) 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 Now I want to sort them according column b, but only its number is considered: b x y z 1 chr1 D 3 1 2 chr13 C 9 2 3 chr15 A 9 1 4 chr2 A 8 1 thanks jian [[alternative HTML version deleted]] __ R-help@r-project.org http://mc/compose?to=r-h...@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. [[alternative HTML version deleted]] __ R-help@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.
Re: [R] sort a data.frame
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. chr1chr2chr9chr10...chr22chrA...chrXchrY if I have dataframe 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)) 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 A 9 1 --- On Thu, 20/5/10, Jorge Ivan Velez jorgeivanve...@gmail.com wrote: From: Jorge Ivan Velez jorgeivanve...@gmail.com Subject: Re: [R] sort a data.frame To: Yuan Jian jayuan2...@yahoo.com Cc: r-help@r-project.org Received: Thursday, 20 May, 2010, 1:31 PM 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, 1, 2)) 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 Now I want to sort them according column b, but only its number is considered: b x y z 1 chr1 D 3 1 2 chr13 C 9 2 3 chr15 A 9 1 4 chr2 A 8 1 thanks jian [[alternative HTML version deleted]] __ R-help@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. [[alternative HTML version deleted]] __ R-help@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.