Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.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,我试图对一个数据帧列进行计算,但它们一直失败,因为尽管我使用了droplevels命令(来自post),该列仍包含级别。我在这里做错了什么: csv <- data.frame(col1 = c("question",1,23,2,5,6), col2 = c("question",5,6,7,3,"")) csv[csv==''] <- NA csv <- csv[-c(1),] #remove the header question row because this scr

我试图对一个数据帧列进行计算,但它们一直失败,因为尽管我使用了droplevels命令(来自post),该列仍包含级别。我在这里做错了什么:

csv <- data.frame(col1 = c("question",1,23,2,5,6), col2 = c("question",5,6,7,3,""))
csv[csv==''] <- NA
csv <- csv[-c(1),] #remove the header question row because this screws up numeric calculations
csv <- droplevels(csv)
csv[,1] <- 7-csv[,1]

降低级别是另一种命令。你不再需要这些因素了。尝试
as.numeric(as.character(mycl))
为算术准备列

csv[] <- lapply(csv, function(x) as.numeric(as.character(x)))

当我们有未使用的因素时,我们会降低水平。不要把它们转换成数字。例如:

fac <- factor(c("a", "b")) #factor with two levels 'a' and 'b'
fac
#[1] a b
#Levels: a b

fac.one <- fac[1] #Just the first element of 'fac' which is 'a'.
fac.one
#[1] a
#Levels: a b       # <-- There are still two levels. 'b' is not used.

降低级别是另一种命令。你不再需要这些因素了。尝试
as.numeric(as.character(mycl))
为算术准备列

csv[] <- lapply(csv, function(x) as.numeric(as.character(x)))

当我们有未使用的因素时,我们会降低水平。不要把它们转换成数字。例如:

fac <- factor(c("a", "b")) #factor with two levels 'a' and 'b'
fac
#[1] a b
#Levels: a b

fac.one <- fac[1] #Just the first element of 'fac' which is 'a'.
fac.one
#[1] a
#Levels: a b       # <-- There are still two levels. 'b' is not used.
droplevels(fac.one)
#[1] a
#Levels: a     #One factor remains. 'b' is dropped