在数据框的列中将因子值转换为数值 [英] Convert factor value into numeric in a column of dataframe

查看:91
本文介绍了在数据框的列中将因子值转换为数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据帧,每行中存储了两个字符串字符

I have a data frame that has two string characters stored in each row

s   ['64.0', '2']   
a   ['63.0', '2']   
b   ['63.0', '1']   

如何将第一个字符串转换为数字值,然后省略第二个字符串,结果将进入数据框,如下所示:

How to convert the first character string into numeric value,and omit the second character string,which results into data frame as follows :

s    64.0   
a    63.0
b    63.0   


推荐答案

我们可以使用 parse_number

library(dplyr)
library(readr)
df2 <-  df1 %>%
          mutate(col2 = parse_number(as.character(col2)))
df2
#   col1 col2
#1    s   64
#2    a   63
#3    b   63






或将 base R sub

as.numeric( sub("\\D+([0-9.]+)[^0-9]+.*", "\\1", df1$col2))



数据



data

df1 <- structure(list(col1 = c("s", "a", "b"), col2 = structure(3:1, .Label = c("['63.0', '1']", 
"['63.0', '2']", "['64.0', '2']"), class = "factor")), row.names = c(NA, 
-3L), class = "data.frame")

这篇关于在数据框的列中将因子值转换为数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆