Reorder rows conditional on a string variable

南楼画角 提交于 2019-12-23 17:25:02

问题


I need to reorder a data frame similar to the one below. I need London to appear first in any different Var, but it is quite critical to keep the var order as it is ("pop, gdp,lifespec...) as I am doing matrix algebra afterwards.

    City      Var value
 Chicago      pop  0.08
  London      pop  0.24
   Paris      pop  0.75
 Chicago      gdp  0.55
  London      gdp  0.49
   Paris      gdp  0.23
 Chicago lifespec  0.45
  London lifespec  0.39
   Paris lifespec  0.28
 Chicago percjobs  0.12
  London percjobs  0.13
   Paris percjobs  0.01

So my desired output would be like the following:

    City      Var value
  London      pop  0.24
 Chicago      pop  0.08
   Paris      pop  0.75
  London      gdp  0.49
 Chicago      gdp  0.55
   Paris      gdp  0.23
  London lifespec  0.39
 Chicago lifespec  0.45
   Paris lifespec  0.28
  London percjobs  0.13
 Chicago percjobs  0.12
   Paris percjobs  0.01

I tried to create a df$rank with values of 1 to London and 9 else. Then I tried to use sort(), but all London values collapse on the top. Do you have any idea?


回答1:


Harvesting the comments on the question here to provide a simple two-liner.

d <- read.table(text='City Var value
 Chicago      pop  0.08
  London      pop  0.24
   Paris      pop  0.75
 Chicago      gdp  0.55
  London      gdp  0.49
   Paris      gdp  0.23
 Chicago lifespec  0.45
  London lifespec  0.39
   Paris lifespec  0.28
 Chicago percjobs  0.12
  London percjobs  0.13
   Paris percjobs  0.01', header=T)

d$Var <- factor(d$Var, unique(d$Var))
d[order(d$Var, d$City != "London"), ]

#       City      Var value
# 2   London      pop  0.24
# 1  Chicago      pop  0.08
# 3    Paris      pop  0.75
# 5   London      gdp  0.49
# 4  Chicago      gdp  0.55
# 6    Paris      gdp  0.23
# 8   London lifespec  0.39
# 7  Chicago lifespec  0.45
# 9    Paris lifespec  0.28
# 11  London percjobs  0.13
# 10 Chicago percjobs  0.12
# 12   Paris percjobs  0.01



回答2:


df$City <- factor(df$City, levels = c('London', 'Chicago', 'Paris'))
df$Cityf <- as.numeric(df$City)
df$Var <- factor(df$Var, levels = c('pop', 'gdp', 'lifespec', 'percjobs'))
df$Varv <- as.numeric(df$Var)
df[order(df$Varv, df$Cityf), ]
df1 <- df[order(df$Varv, df$Cityf), ]
df1[,c(1,2,3)]

      City      Var value
2   London      pop  0.24
1  Chicago      pop  0.08
3    Paris      pop  0.75
5   London      gdp  0.49
4  Chicago      gdp  0.55
6    Paris      gdp  0.23
8   London lifespec  0.39
7  Chicago lifespec  0.45
9    Paris lifespec  0.28
11  London percjobs  0.13
10 Chicago percjobs  0.12
12   Paris percjobs  0.01

I'm using dplyr_0.2 notation



来源:https://stackoverflow.com/questions/24035379/reorder-rows-conditional-on-a-string-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!