0

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)

tom
  • 725
  • 4
  • 17
  • If it's a data.table do `df1[, mget(df2$names)]` . Possible duplicate of https://stackoverflow.com/questions/30562602/access-data-table-columns-with-strings – Ronak Shah Aug 30 '19 at 10:55
  • 2
    Please make your question more reproducible by adding current and expected output. Provide sample data with `dput(head(df,n))`. – NelsonGon Aug 30 '19 at 11:41
  • argument name is `neworder` not `key`.... – s_baldur Aug 30 '19 at 13:00
  • Dear Sindri Baldur, Thank you for your reply. I have tried df3<-setcolorder(df1, neworder=as.character(df2$names)) and R has returned with an error message "Names in neworder not found in x", although there are the names in x, the same as in df2$names. – Jakhongir Alidjanov Aug 30 '19 at 14:58
  • Dear NelsonGon, thank very much for your reply. I am not sure that dput(head(df,n)) could work here, because my data set consists of 2087 observations of 183 variables. – Jakhongir Alidjanov Aug 30 '19 at 15:03

2 Answers2

1

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)).

Benjamin Ye
  • 508
  • 2
  • 7
  • Dear Benjamin, thank you for your reply. Actually, the class of variables in df2$names is already set as a character vector. However, I have used as.character(df2$names), but the R has returned an error message "Names in neworder not found in x", although there are the names in x, the same as in df2$names. – Jakhongir Alidjanov Aug 30 '19 at 14:52
0

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.

David Arenburg
  • 91,361
  • 17
  • 137
  • 196