paulgr
paulgr

Reputation: 89

Moving rows in a data frame

I'm trying to use the move_columns function from the sjmisc package. However I'm having a problem in that when I use numerals I get a different result from the one I get when I use variables standing for those indices. For example I want to move the column Petal.Width to position 3 (so after 2), but when I use variables it gets moved to the end of the data frame.

> library(sjmisc)
> 
> data(iris)
> head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa
> 
> index_rec<-4
> index<-2
> one<-move_columns(iris,index_rec,.after=index)
> head(one)
  Sepal.Length Sepal.Width Petal.Length Species Petal.Width
1          5.1         3.5          1.4  setosa         0.2
2          4.9         3.0          1.4  setosa         0.2
3          4.7         3.2          1.3  setosa         0.2
4          4.6         3.1          1.5  setosa         0.2
5          5.0         3.6          1.4  setosa         0.2
6          5.4         3.9          1.7  setosa         0.4
> 
> two<-move_columns(iris,4,.after=2)
> head(two)
  Sepal.Length Sepal.Width Petal.Width Petal.Length Species
1          5.1         3.5         0.2          1.4  setosa
2          4.9         3.0         0.2          1.4  setosa
3          4.7         3.2         0.2          1.3  setosa
4          4.6         3.1         0.2          1.5  setosa
5          5.0         3.6         0.2          1.4  setosa
6          5.4         3.9         0.4          1.7  setosa

The documentation says that if neither of .before or .after are specified, the column is moved to the end of the data frame by default. So is the problem in the first case that I'm not specifying .after? I think it's clearly there...

Upvotes: 1

Views: 369

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388797

EDIT

It works with quasi-quotation.

library(sjmisc)

index_rec<-4
index <- 2

move_columns(iris,4,.after=!!index) %>% head

#  Sepal.Length Sepal.Width Petal.Width Petal.Length Species
#1          5.1         3.5         0.2          1.4  setosa
#2          4.9         3.0         0.2          1.4  setosa
#3          4.7         3.2         0.2          1.3  setosa
#4          4.6         3.1         0.2          1.5  setosa
#5          5.0         3.6         0.2          1.4  setosa
#6          5.4         3.9         0.4          1.7  setosa

Earlier Answer

It seems like a bug to me when you pass the number as a variable to the function.

#This works fine
move_columns(iris,4,.after=2) %>% head

# Sepal.Length Sepal.Width Petal.Width Petal.Length Species
#1          5.1         3.5         0.2          1.4  setosa
#2          4.9         3.0         0.2          1.4  setosa
#3          4.7         3.2         0.2          1.3  setosa
#4          4.6         3.1         0.2          1.5  setosa
#5          5.0         3.6         0.2          1.4  setosa
#6          5.4         3.9         0.4          1.7  setosa

#This doesn't
move_columns(iris,4,.after=index) %>% head

#  Sepal.Length Sepal.Width Petal.Length Species Petal.Width
#1          5.1         3.5          1.4  setosa         0.2
#2          4.9         3.0          1.4  setosa         0.2
#3          4.7         3.2          1.3  setosa         0.2
#4          4.6         3.1          1.5  setosa         0.2
#5          5.0         3.6          1.4  setosa         0.2
#6          5.4         3.9          1.7  setosa         0.4

Why not use the new relocate function from dplyr? It does not have a bug and works as expected when passed a variable.

library(dplyr)

relocate(iris, 4, .after=2) %>% head

#  Sepal.Length Sepal.Width Petal.Width Petal.Length Species
#1          5.1         3.5         0.2          1.4  setosa
#2          4.9         3.0         0.2          1.4  setosa
#3          4.7         3.2         0.2          1.3  setosa
#4          4.6         3.1         0.2          1.5  setosa
#5          5.0         3.6         0.2          1.4  setosa
#6          5.4         3.9         0.4          1.7  setosa

relocate(iris,index_rec,.after=index) %>% head

#  Sepal.Length Sepal.Width Petal.Width Petal.Length Species
#1          5.1         3.5         0.2          1.4  setosa
#2          4.9         3.0         0.2          1.4  setosa
#3          4.7         3.2         0.2          1.3  setosa
#4          4.6         3.1         0.2          1.5  setosa
#5          5.0         3.6         0.2          1.4  setosa
#6          5.4         3.9         0.4          1.7  setosa

Upvotes: 1

Related Questions