从Messy数据中将因子更改为数字

时间:2017-11-18 03:14:21

标签: r dplyr stringr

假设:

df <- data.frame(Price = c(1,2,3,4,5),
   Percent = c("5%","6%","2%","3%","4%"))

Percent变量是一个因子:str(百分比)时输出中的“5%”,“6%”等。我无法使用上面的示例重现它。我正在尝试将这些数据转换为数字但无法弄清楚如何。

我尝试了很多变化:

as.numeric(as.character(Percent)) #All NAs result because of the ""

然后我试着修剪“”,但无法弄清楚如何

as.character(df$Percent) #Transform to character to use StringR
string_replace(df$Percent, "", ) #Does not run

如何从数据中删除“”?

0 个答案:

没有答案
相关问题