我有一个因子变量,它包含整数和字符值。我想只提取整数值。 我试试我的代码:
m <- c(k[,1])
m <- as.vector.factor(k[,1])
h <- c()
for (k in m) {
if(!is.na(suppressWarnings(as.numeric(k)))) {
h<-c(h,k)
}
}
h <- as.numeric(h)
h
它工作正常,但我认为还有另一种更好的方法。你有什么建议吗?我怎样才能做得更好?
m<-as.vector.factor(k[,1])
m
[1] "Text" "Test: Text, Text" "Text"
[4] "Text" "" ""
[7] "1990" "1991" "1992"
[10] "1993" "1994" "1995"
[13] "1996" "1997" "1998"
[16] "1999" "2000" "2001"
[19] "2002" "2003" "2004"
[22] "2005" "2006" "2007"
[25] "2008" "2009" "2010"
[28] "2011" "2012" "2013"
[31] "2014" "2015" "__________"
[34] "Text" "Text"