Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中同步排序2个不同的列表?_R_List_Sorting - Fatal编程技术网

如何在R中同步排序2个不同的列表?

如何在R中同步排序2个不同的列表?,r,list,sorting,R,List,Sorting,我有名单 examplevar1 = [x1, x3, x2] examplenum1 = [0.1, -5, 2.4] 我需要同步地对两者进行排序,以使结果是一致的 examplevar1 = [x1, x2, x3] examplenum1 = [01, 2.4, -5] 我在键入时读到了关于``sort()``函数的内容,但在这种情况下它似乎不起作用 在这里等待答案的时候,我也会尝试一些东西 谢谢 编辑:添加将在其上使用的代码 fixAllwMatrix = function(smbl

我有名单

examplevar1 = [x1, x3, x2]
examplenum1 = [0.1, -5, 2.4]
我需要同步地对两者进行排序,以使结果是一致的

examplevar1 = [x1, x2, x3]
examplenum1 = [01, 2.4, -5]
我在键入时读到了关于``sort()``函数的内容,但在这种情况下它似乎不起作用

在这里等待答案的时候,我也会尝试一些东西

谢谢

编辑:添加将在其上使用的代码

fixAllwMatrix = function(smbls,numbs,matrixx){
  
  count = 0
  for (itema in smbls) {
    count = count + 1
    if(identical(toString(itema),"+")){
      smbls = smbls[(0-count)]
      count = count - 1
      
    }
    if(identical(toString(itema),"*")){
      smbls = smbls[(0-count)]
      count = count - 1
    }
    if(identical(toString(itema),"-")){
      numbs[count] = numbs[count] * -1
      smbls = smbls[(0-count)]
      count = count - 1
    }
    
  }
  print(smbls)
  for (a in smbls) {
    print(typeof(a))
    a = toString(a)
    print(typeof(a))
  }
  df = data.frame(var1 = smbls, num1 = numbs)
  df1 = df[order(as.numeric(gsub('\\D', '', smbls))), ]
  print(df1)
其中:
smbls
是变量(smbls
for
循环用于删除列表中的+-和*项。在
for
循环期间,列表项的类型为“符号”
numbs
是给定的数字 循环的最后一个
是将smbls项转换为字符串
然后在最后一个pert,数据帧部分,我得到了错误

Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  : 
  object 'x1' not found

有解决方法吗?

您可以将这两个向量放在一个数据帧中,然后使用
gtools::mixedorder

examplevar1 = c('x1', 'x3', 'x2')
examplenum1 = c(0.1, -5, 2.4)
df <- data.frame(var1 = examplevar1, num1 = examplenum1)
df1 <- df[gtools::mixedorder(df$var1), ]

#  var1 num1
#1   x1  0.1
#3   x2  2.4
#2   x3 -5.0
examplevar1=c('x1','x3','x2')
示例num1=c(0.1,-5,2.4)

抱歉5小时后回复。我正忙着做一些事情。我尝试了你的答案,包括
gtools
和base R的答案,但我一直在(函数)中得到错误
error(…,row.names=NULL,check.rows=FALSE,check.names=TRUE,:object'x1'未找到
。是否有问题需要解决?能否提供一个可复制的示例供我测试此问题?当前数据不可复制。我将编辑我使用的代码我添加了我使用的函数我们需要的数据多于代码。请添加问题中的
dput(df)
输出。或者您也可以添加
dput(smbls)
dput(numbs)
df1 <- df[order(as.numeric(gsub('\\D', '', examplevar1))), ]