R: converting some levels of a factor to numeric

I am new to R. I have a factor object with >10000 levels. Most of these levels are numbers which I would like to convert to numeric, but there are several levels that contain descriptions which I do not wish to touch.

Is there a good way for me to isolate the relevant levels of the factor to convert into numeric? Would as.numeric() be a good choice?

Many thanks!


由于我们不能有一个具有多个类的列/向量,因此一个建议是仅将numeric元素进行子集化并使用as.numeric(as.character(将其转换回numeric as.numeric(as.character(

i1 <- grepl("^[0-9.]+$", v1)
v2 <- as.numeric(as.character(v1[i1]))
链接地址: http://www.djcxy.com/p/24950.html

上一篇: 如何将具有缺失值的因子变量转换为数值变量?

下一篇: R:将因子的某些级别转换为数字