Is it possible to swap columns around in a data fr

2019-01-17 16:12发布

问题:

I have three variables in a data frame and would like to swap the 4 columns around from

"dam"   "piglet"   "fdate"   "ssire"

to

"piglet"   "ssire"   "dam"   "tdate"

Is there any way I can do the swapping using R?

Any help would be very much appreciated.

Baz

回答1:

dfrm <- dfrm[c("piglet", "ssire", "dam", "tdate")]

OR:

dfrm <- dfrm[ , c("piglet", "ssire", "dam", "tdate")]


回答2:

d <- data.frame(a=1:3, b=11:13, c=21:23)
d
#  a  b  c
#1 1 11 21
#2 2 12 22
#3 3 13 23
d2 <- d[,c("b", "c", "a")]
d2
#   b  c a
#1 11 21 1
#2 12 22 2
#3 13 23 3

or you can do same thing using index:

d3 <- d[,c(2, 3, 1)]
d3
#   b  c a
#1 11 21 1
#2 12 22 2
#3 13 23 3


回答3:

To summarise the other posts, there are three ways of changing the column order, and two ways of specifying the indexing in each method.

Given a sample data frame

dfr <- data.frame(
  dam    = 1:5,
  piglet = runif(5),
  fdate  = letters[1:5],
  ssire  = rnorm(5)
)

Kohske's answer: You can use standard matrix-like indexing using column numbers

dfr[, c(2, 4, 1, 3)]

or using column names

dfr[, c("piglet", "ssire", "dam", "fdate")]

DWin & Gavin's answer: Data frames allow you to omit the row argument when specifying the index.

dfr[c(2, 4, 1, 3)]
dfr[c("piglet", "ssire", "dam", "fdate")]

PaulHurleyuk's answer: You can also use subset.

subset(dfr, select = c(2, 4, 1, 3))
subset(dfr, select = c(c("piglet", "ssire", "dam", "fdate")))


回答4:

You can use subset's 'select' argument;

#Assume df contains "dam" "piglet" "fdate" "ssire"

newdf<-subset(df, select=c("piglet", "ssire", "dam", "tdate"))


回答5:

I quickly wrote a function that takes a vector v and column indexes a and b which you want to swap.

swappy = function(v,a,b){  # where v is a dataframe, a and b are the 
columns indexes to swap

name = deparse(substitute(v))

helpy = v[,a]
v[,a] = v[,b]
v[,b] = helpy


name1 = colnames(v)[a] 
name2 = colnames(v)[b] 

colnames(v)[a] = name2
colnames(v)[b] = name1

assign(name,value = v , envir =.GlobalEnv)
}


标签: r swap