Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/84.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,我需要把向量转换成数字字符串。我尝试了中的解决方案,但收到错误消息 例如,使用此数据集: v1 <- rep(c('a','b'),3) v2 <- rep(c('c','d','e'),2) vdf <- data.frame(v1, v2) sapply(vdf, class) v1 v2 "factor" "factor" sapply(vdf, mode) #I'm not sure why the mode shows as numer

我需要把向量转换成数字字符串。我尝试了中的解决方案,但收到错误消息

例如,使用此数据集:

v1 <- rep(c('a','b'),3)
v2 <- rep(c('c','d','e'),2)
vdf <- data.frame(v1, v2)

sapply(vdf, class)
      v1       v2 
"factor" "factor" 

sapply(vdf, mode) #I'm not sure why the mode shows as numeric as the value is a charecter
       v1        v2 
"numeric" "numeric" 

v1多亏了Sotos的评论,我才能够轻松解决这个问题。根据他的评论,对我有效的解决方案是


vdf[]
vdf[]有效。谢谢Sotos
vdf
v1c <- rep(c(1,2),3)
v2c <- rep(c(1,2,3),2)
vdfc <- data.frame(v1c, v2c)

sapply(vdfc, class)
      v1c       v2c 
"numeric" "numeric" 
     sapply(vdfc, mode)
      v1c       v2c 
"numeric" "numeric" 
df$iv1 = as.integer(df$v1) 
df$iv1 = as.integer(as.factor(df$v1))

Error in `$<-.data.frame`(`*tmp*`, "iv1", value = integer(0)) : replacement has 0 rows, data has 10