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[]有效。谢谢Sotosvdf
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