Reputation: 15
I have a dataframe in R. I'm ordering the columns alphabetically like this:
df_ordered <- df[,order(names(df))]
Now I want to select a specific column by name (column name is "city") and "pull" it to the front while leaving the rest unchanged, i.e. if the columns are:
alpha, beta, city, delta
I want them to be
city, alpha, beta, delta
Thanks for your help.
Upvotes: 0
Views: 94
Reputation: 206382
Using @nrussell sample Df
data.frame, you can also do a trick with relevel
.
Df[, levels(relevel(factor(names(Df)),"G"))]
We use the implicit sorting of factor()
and use relevel
to bring a particular column forward.
Upvotes: 3
Reputation: 1647
We can define a function which returns your desired column subsettor.
pullToFront<-function(entry, vect){
c(entry, vect[vect!=entry])
}
Where vect is a vector of your column names, entry is the specific column you want to move to the front.
Say you have the following column names.
vect<-letters[1:5]
vect
#output
#[1]"a" "b" "c" "d" "e"
We want to pull "c" to the front, so we say
subsettor<-pullToFront("c", vect)
subsettor
#output
#[1]"c" "a" "b" "d" "e"
Then given a data frame with those column names, you would simply say
df[,subsettor]
You would use this function by saying
vect<-colnames(df)
Then isolating the one you want to move to the front in whatever way you choose
Upvotes: 0
Reputation: 18612
How about this:
Df <- data.frame(
matrix(rnorm(52),
ncol=26))
names(Df) <- LETTERS
##
Df <- Df[,c("C",sort(names(Df)[names(Df)!="C"]))]
##
> head(Df)
C A B D E F G H I J K
1 -1.1685829 -1.0733238 0.1991507 -0.5315889 1.417376 0.3473343 0.8068851 -1.3548045 1.5587990 0.1617466 -0.02262477
2 -0.9241126 -0.3910728 1.6685727 -0.7506446 -1.275201 -0.1603301 0.9326244 -0.8349584 0.4534975 -0.3170741 0.63663999
L M N O P Q R S T U V
1 0.9524257 -0.04871767 -0.8686760 -1.971867 2.0080797 -0.6866727 0.4532914 -0.328414 0.05844098 0.8595681 1.1198372
2 1.7291060 1.52756996 0.4328425 1.560806 -0.4236301 -1.1098159 0.3315950 1.328299 2.34595615 -0.3624254 -0.2300019
W X Y Z
1 -0.2515527 -0.2934354 -0.9494282 -0.9744378
2 1.0197277 -0.6822827 -1.0165435 -0.3204356
Or if you prefer to sort the columns before hand, like in your example, the sort
is not needed:
Df <- data.frame(
matrix(rnorm(52),
ncol=26))
names(Df) <- sample(LETTERS,26)
Df <- Df[,order(names(Df))]
Df <- Df[,c("C",names(Df)[names(Df)!="C"])]
##
> head(Df)
C A B D E F G H I J K
1 -0.4508683 0.605652 1.0056819 0.3410927 -0.08246848 0.4173749 1.7259750 1.0860495 0.06465027 -1.5329574 0.1268806
2 0.5212941 1.264273 -0.3907112 0.4043759 -0.76360427 -0.7712454 -0.2604928 -0.8549233 0.53874782 0.1891443 1.9644381
L M N O P Q R S T U V
1 0.8127268 -0.9294953 -0.9134001 2.0891866 -0.1560481 1.480088 0.8328550 -0.47946694 -0.55311500 -0.1393134 0.7400875
2 -0.9649869 -0.6869047 -0.8529926 0.1224387 -0.4958187 -1.273009 0.2728192 0.07974988 -0.01028435 0.9705935 -0.8299274
W X Y Z
1 -1.9356416 0.9440026 0.9074300 -1.082567
2 -0.2265877 0.4264449 0.9681043 -1.819203
Upvotes: 0