0

我想根据另一个数据框列中给出的文本变量对数据框的列进行排序(使用srtcolorderfrom data.table)。假设我有第一个数据框(df1)列命名和排序为“五”、“二”、“三”、“一”、“四”,并且有另一个数据框的列(df2$names),包含字符串变量排序为“一”、“二”、“三”、“四”、“五”。如何根据中的变量对 df1 的列进行排序df2$names

我尝试使用setcolorder如下:

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

并收到一条错误消息

setcolorder(df1, key = df2$names) 中的错误:未使用的参数 (key = df2$names)

4

2 回答 2

1

由于没有提供数据,我创建了虚拟数据框来模拟您尝试重新排列的数据。

第一个数据框 ,df1包含字符列five, two, three, one, 并按four以下顺序:

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

第二个数据框df2包含一个标题names为列名的列df1,按其等效数字排序。

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

可以使用包含列索引或列名称的有序向量对数据框进行排序。在这种情况下,我们可以调用df1[, as.character(df2$names)]sortdf1的列。as.character()用于将df1列名的因子向量转换为字符串向量。

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

于 2019-08-30T11:55:21.830 回答
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.

于 2019-08-30T16:38:42.100 回答