Jakhongir Alidjanov
Jakhongir Alidjanov

Reputation: 109

How to order columns of the data frame according to the string variables taken from the column

I would like to order the columns of the data frame according to the text variables given in the column of another data frame (using srtcolorder from the data.table). Let's say, that i have first data frame (df1) with columns named and ordered as "five", "two" "three", "one", "four", and have another data frame with the column (df2$names), containing the string variables ordered as "one", "two", "three", "four", "five". How can I order the columns of the df1, according to the variables in df2$names?

I have tried to use setcolorder as follows:

gf3<-setcolorder(df1, key=df2$names)

And get an error message

Error in setcolorder(df1, key = df2$names) : unused argument (key = df2$names)

Upvotes: 0

Views: 695

Answers (2)

Jakhongir Alidjanov
Jakhongir Alidjanov

Reputation: 109

I thank you all for your replies. I understood my mistake. There where occasionally some spaces before the strings in the df2$names, which could not be seen on the screen. Therefore, text in this column did not match the column names in df1. I have remembered a similar problem which I had with the "invisible" spaces and just intuitively tried gsub(" ", "" df2$names) to remove them. Thereafter, df3<-setcolorder(df1, neworder=key(as.character(df2[["names"]])) worked perfectly. Thank you all again.

Upvotes: 0

Benjamin Ye
Benjamin Ye

Reputation: 518

Since no data was provided, I created dummy data frames to simulate the data you are trying to rearrange.

The first data frame, df1, contains the character columns five, two, three, one, and four in that order:

df1 <- data.frame(
  five = character(),
  two = character(),
  three = character(),
  one = character(),
  four = character()
)

The second data frame, df2, contains a single column titled names with the column names in df1 sorted by their numerical equivalents.

df2 <- data.frame(names = c('one', 'two', 'three', 'four', 'five'))

Data frames can be sorted with either an ordered vector containing the column indices or the column names. In this case, we can call df1[, as.character(df2$names)] to sort df1's columns. as.character() is used to convert the factor vector of df1 column names into a string vector.

If you are keen on using data.table::setcolorder(), you can call setcolorder(df1, as.character(df2$names)) instead. A benefit of this method is that you do not have to assign the resulting data frame to the variable df1. Your attempt did not work because (1) there is no key parameter for the setcolorder() function (there is only neworder) and (2) your df2$names was likely a factor vector (you can check by calling class(df2$names)).

Upvotes: 1

Related Questions