Sort each row in a dataframe by value of column

Say I have a dataframe like this:

Date A B C D E H 1/28/2013 56 51 35 44 08 18 1/25/2013 38 56 28 39 23 32 1/21/2013 36 51 45 25 40 08

what I want to do is sort each row's ABCDE column by their values. So I can get is:

Date A B C D E H 1/28/2013 08 35 44 51 56 18 1/25/2013 23 28 38 39 56 32 1/21/2013 25 36 40 45 51 08

Answer1:

You can use apply:

dat[c("A", "B", "C", "D", "E")] <- t(apply(dat[c("A", "B", "C", "D", "E")], 1, sort)) # Date A B C D E H # 1 1/28/2013 8 35 44 51 56 18 # 2 1/25/2013 23 28 38 39 56 32 # 3 1/21/2013 25 36 40 45 51 8

where dat is the name of your data frame.

人吐槽 人点赞

Recommend

Comment

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

查看评论:Sort each row in a dataframe by value of column