简体   繁体   English

如何将所有非数字值转换为数据帧中R中的NA?

[英]How do I convert all nonumeric values to NAs in R in a dataframe?

I have a dataframe called returns. 我有一个名为return的数据框。 How do I make the code below more concise? 如何使以下代码更简洁? I tried using apply, lappply and sapply, but they seem to change the structure of the dataframe such that I cannot produce a correlation matrix. 我尝试使用apply,lappply和sapply,但是它们似乎改变了数据框的结构,因此我无法生成相关矩阵。 To clarify: the code below works, but I would like to make it more concise. 需要澄清的是:下面的代码有效,但是我想使其更加简洁。 Thanks! 谢谢!

returns$VIT <- as.numeric(as.character(returns$VIT))
returns$EFA <- as.numeric(as.character(returns$EFA))
returns$VWO <- as.numeric(as.character(returns$VWO))
returns$VIG <- as.numeric(as.character(returns$VIG))
returns$VNQ <- as.numeric(as.character(returns$VNQ))
returns$iPath.DJP <- as.numeric(as.character(returns$iPath.DJP))
returns$iShares.MUB <- as.numeric(as.character(returns$iShares.MUB))

cor(returns, use="pairwise.complete.obs")

EDIT: The following test code does NOT work, I am trying to figure out how to make it work. 编辑:以下测试代码不起作用,我试图弄清楚如何使其工作。

test <- data.frame(c(.04,.2,"blah"),c(.01,.24,"blah"))
colnames(test) <- c("VIT", "EFA")

new <- apply(test, 2, function(x) as.numeric(as.character(x)))
cor(test, use="pairwise.complete.obs")
test <- data.frame(a = rep("bob", 12), b = rep(c(1,23,4), times = 4))
Filter(is.numeric, test)

Returns only the numeric columns in test . 仅返回test的数字列。 You can use lapply(test, is.numeric) to get the true/false results yourself and do something with that. 您可以使用lapply(test, is.numeric)自己获取对/错结果,并对此进行处理。

Is this what you're looking for? 这是您要找的东西吗?

numeric.columns <- c('VIT','EFA','VWO','VIG','VNQ','iPath.DJP','iPath.DJP','iShares.MUB')
returns[,numeric.columns] <- lapply(returns[,numeric.columns], function(x) as.numeric(as.character(x)))
cor(returns, use="pairwise.complete.obs")

Just use plyr 's colwise . 只需使用plyrcolwise

returns = colwise(function(x) as.numeric(ifelse(is.numeric(x),x,NA)))(returns)

It takes a function and applies it to all columns. 它接受一个函数并将其应用于所有列。 The final result should be numeric. 最终结果应该是数字。 Note that this treats values such as '1' as NA. 请注意,这会将'1'值视为NA。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在R中将所有NA都转换为0? - How do you convert all the NAs with 0 in R? 如何创建一个向量,用 R 中的某个值替换所有值 - How do I create a vector which replaces all values with NAs and NAs with a certain value in R R:如何使用来自利用其他多列的条件的值替换 dataframe 列中的 NA? - R: How do I replace NAs in a dataframe column with values from conditions leveraging other multiple columns? 我如何在R中打印数据框将所有十进制值 - How do I print a dataframe in R will all decimal values 如何删除R中数据框列中字符串中的所有NA? - How to remove all NAs in character strings in a dataframe column in R? 如何在R中的数据帧中将多种类型的值替换为NA - How to replace multiple type of values to NAs in a dataframe in R 如何用 NA 替换我的数据框中包含字符串“x”的所有值? - How to replace all values in my dataframe containing the string "x" with NAs? 如何将列中的最后一个值附加到R中的所有NA? - How to append last values in a column to all NAs in R? 如果“下标赋值中不允许使用 NA”,我如何将表的值与 R 中更长的名称列表进行比较和匹配? - If "NAs are not allowed in subscripted assignments", how do I compare and match values of a table to a longer list of names in R? 对于 R 中的缺失值,如何获取一个列的子集并将其放入一个包含 0 而不是 NA 的新列中? - How do I take a subset of a column and make it into a new column with 0s instead of NAs for missing values in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM