Reorder rows conditional on a string variable

2019-07-07 08:29发布

问题:

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



标签: r sorting row