Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.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中的整数_R - Fatal编程技术网

排序将列表更改为R中的整数

排序将列表更改为R中的整数,r,R,我有一个列表,当我应用sort()时,它将类型更改为“integer”,这是我无法理解的。非常感谢你的帮助 myfile.csv是一个值为{“a”、“a”、“c”、“b”、“c”、“a”}的单列 代码如下: temp <- read.csv("myfile.csv",header=TRUE) typeof(temp) ## prints: "list" temp2 <- sort(temp[,1]) typeof(temp2) ## prints: "integer" temp2t

我有一个列表,当我应用sort()时,它将类型更改为“integer”,这是我无法理解的。非常感谢你的帮助

myfile.csv是一个值为{“a”、“a”、“c”、“b”、“c”、“a”}的单列

代码如下:

temp <- read.csv("myfile.csv",header=TRUE)
typeof(temp) ## prints: "list"
temp2 <- sort(temp[,1])
typeof(temp2) ## prints: "integer"

temp2
temp2使用此命令,
temp2
将是具有排序值的数据帧:

temp2 <- temp[order(temp[ , 1]), , drop = FALSE]

temp2使用此命令,
temp2
将是一个具有排序值的数据帧:

temp2 <- temp[order(temp[ , 1]), , drop = FALSE]

temp2为什么不在
temp
上使用
dput()?目前,您的代码不是完全可复制的。您的
temp
似乎是一个
数据帧(
typeof(temp)=“list”
),但您只对第一列进行排序(
sort(temp[,1])
),这可能是一个
因素。而
因子
的类型为
整数
。所以一切都是对的,但可能您的期望不同。temp是一个只有1列的data.frame。因此,当我检查typeof(temp)时,我得到一个列表。我的目标是使用temp2[1,]比较此列表的“排序”版本的连续行元素。我不是在错误地进行排序,就是错误地引用了已排序的版本。为什么不在
temp
上使用
dput()
,让我们看看您在找什么?目前,您的代码不是完全可复制的。您的
temp
似乎是一个
数据帧(
typeof(temp)=“list”
),但您只对第一列进行排序(
sort(temp[,1])
),这可能是一个
因素。而
因子
的类型为
整数
。所以一切都是对的,但可能您的期望不同。temp是一个只有1列的data.frame。因此,当我检查typeof(temp)时,我得到一个列表。我的目标是使用temp2[1,]比较此列表的“排序”版本的连续行元素。我要么是以错误的方式进行排序,要么是错误地引用了排序后的版本。我这样做了,我创建了一个包含三个元素temp2[1],temp2[2],temp2[3]的向量,而不是“a”,“a”,“c”,我得到了1,2,3,你有因子。用
temp读取数据,我这样做了,我用三个元素temp2[1],temp2[2],temp2[3]创建了一个向量,而不是“a”,“a”,“c”,我得到了1,2,3,你有因子。使用
temp读取中的数据这非常有效!谢谢然而,这里有一个快速的问题。然后我执行{levels@PreritAhuja不要使用
c
。它将因子值转换为数值。如果执行
级别,这将非常有效!谢谢!但是,这里有一个快速的持续问题。我正在执行{levels@PreritAhuja不使用
c
。它将因子值转换为数值。如果执行
levels