Yehuda
Yehuda

Reputation: 1893

Ordering columns in a dataframe in R

I have created a data frame using the following code:

manager <- c(1,2,3,4,5)
date <- c("10/24/08","10/28/08","10/1/08","10/12/08","5/1/09")
country <- c("US","US","UK","UK","UK")
gender <- c("M","F","F","M","F")
age <- c(32,45,25,39,99)
q1 <- c(5,3,3,3,2)
q2 <- c(4,5,5,3,2)
q3 <- c(5,2,5,3,2)
q4 <- c(5,5,5,NA,2)
q5 <- c(5,5,2,NA,1)
leadership <- data.frame(manager,date,country,gender,age,
                     q1,q2,q3,q4,q5, stringsAsFactors = FALSE)

I have then queried using the following: View(leadership[c(1:3),c("q1","q2","q3","q4","q5")])

My question is as follows: Is there any way to set up an ordered structure to the columns, such that I will be able to query using the following code? View(leadership[c(1:3),c("q1":"q5")])

Thanks in advance.

Upvotes: 0

Views: 88

Answers (2)

Parfait
Parfait

Reputation: 107737

Alternatively, in base R append a list with grep() on column names:

leadership[append(1:3, grep("^q.*$", names(leadership)))]
#   manager     date country q1 q2 q3 q4 q5
# 1       1 10/24/08      US  5  4  5  5  5
# 2       2 10/28/08      US  3  5  2  5  5
# 3       3  10/1/08      UK  3  5  5  5  2
# 4       4 10/12/08      UK  3  3  3 NA NA
# 5       5   5/1/09      UK  2  2  2  2  1

And if just a subset of q's pass in a regex search:

leadership[append(1:3, grep("^q[1-3].*$", names(leadership)))]
#   manager     date country q1 q2 q3
# 1       1 10/24/08      US  5  4  5
# 2       2 10/28/08      US  3  5  2
# 3       3  10/1/08      UK  3  5  5
# 4       4 10/12/08      UK  3  3  3
# 5       5   5/1/09      UK  2  2  2

leadership[append(1:3, grep("^q[4-5].*$", names(leadership)))]
#   manager     date country q4 q5
# 1       1 10/24/08      US  5  5
# 2       2 10/28/08      US  5  5
# 3       3  10/1/08      UK  5  2
# 4       4 10/12/08      UK NA NA
# 5       5   5/1/09      UK  2  1

Upvotes: 1

Gregor Thomas
Gregor Thomas

Reputation: 146070

dplyr::select allows for the non-standard syntax you propose.

library(dplyr)
View(select(leadership[1:3, ], q1:q5))

See ?select for more details. You could also do select(leadership, starts_with("q"))

Upvotes: 0

Related Questions