如何将数据框列转换为数字类型? [英] How to convert a data frame column to numeric type?

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

问题描述

如何将数据框列转换为数字类型?

How do you convert a data frame column to a numeric type?

推荐答案

由于(仍)没有人得到复选标记我假设你有一些实际的问题,主要是因为你没有指定要转换为 numeric 的什么类型的向量。我建议你应用 transform 函数来完成你的任务。

Since (still) nobody got check-mark, I assume that you have some practical issue in mind, mostly because you haven't specified what type of vector you want to convert to numeric. I suggest that you should apply transform function in order to complete your task.

现在我要演示某些转换异常:

Now I'm about to demonstrate certain "conversion anomaly":

# create dummy data.frame
d <- data.frame(char = letters[1:5], 
                fake_char = as.character(1:5), 
                fac = factor(1:5), 
                char_fac = factor(letters[1:5]), 
                num = 1:5, stringsAsFactors = FALSE)

让我们瞥一下 data.frame

> d
  char fake_char fac char_fac num
1    a         1   1        a   1
2    b         2   2        b   2
3    c         3   3        c   3
4    d         4   4        d   4
5    e         5   5        e   5

,让我们运行:

> sapply(d, mode)
       char   fake_char         fac    char_fac         num 
"character" "character"   "numeric"   "numeric"   "numeric" 
> sapply(d, class)
       char   fake_char         fac    char_fac         num 
"character" "character"    "factor"    "factor"   "integer" 

现在你可能会问自己哪里有异常?嗯,我碰到了R中很奇特的东西,这不是 最困惑的事情,但它可能会让你感到困惑,特别是如果你在睡觉之前阅读过这个信息。

Now you probably ask yourself "Where's an anomaly?" Well, I've bumped into quite peculiar things in R, and this is not the most confounding thing, but it can confuse you, especially if you read this before rolling into bed.

这里:前两列是 character 。我故意地叫2 nd 一个 fake_char 。将这个字符变量与Dirk在他的回复中创建的变量相似。它实际上是一个数字矢量转换为字符。 3 rd 和4 th 列是因子,最后一个是纯粹的数字

Here goes: first two columns are character. I've deliberately called 2nd one fake_char. Spot the similarity of this character variable with one that Dirk created in his reply. It's actually a numerical vector converted to character. 3rd and 4th column are factor, and the last one is "purely" numeric.

如果您使用 transform 函数,可以转换 fake_char into numeric ,但不是 char 变量本身。 >

If you utilize transform function, you can convert the fake_char into numeric, but not the char variable itself.

> transform(d, char = as.numeric(char))
  char fake_char fac char_fac num
1   NA         1   1        a   1
2   NA         2   2        b   2
3   NA         3   3        c   3
4   NA         4   4        d   4
5   NA         5   5        e   5
Warning message:
In eval(expr, envir, enclos) : NAs introduced by coercion

但如果您在 fake_char char_fac ,你会很幸运,没有NA:

but if you do same thing on fake_char and char_fac, you'll be lucky, and get away with no NA's:

> transform(d, fake_char = as.numeric(fake_char), 
               char_fac = as.numeric(char_fac))

  char fake_char fac char_fac num
1    a         1   1        1   1
2    b         2   2        2   2
3    c         3   3        3   3
4    d         4   4        4   4
5    e         5   5        5   5

如果您保存已转换的 data.frame 并检查 code>和 class ,你会得到:

If you save transformed data.frame and check for mode and class, you'll get:

> D <- transform(d, fake_char = as.numeric(fake_char), 
                    char_fac = as.numeric(char_fac))

> sapply(D, mode)
       char   fake_char         fac    char_fac         num 
"character"   "numeric"   "numeric"   "numeric"   "numeric" 
> sapply(D, class)
       char   fake_char         fac    char_fac         num 
"character"   "numeric"    "factor"   "numeric"   "integer"

所以,结论是: 是的,您可以将字符矢量转换为 numeric 一个,但只有当元素可转换到 numeric 如果只有一个元素在向量中,尝试将该向量转换为数值一个时会收到错误。

So, the conclusion is: Yes, you can convert character vector into a numeric one, but only if it's elements are "convertible" to numeric. If there's just one character element in vector, you'll get error when trying to convert that vector to numerical one.

只是为了证明我的观点:

And just to prove my point:

> err <- c(1, "b", 3, 4, "e")
> mode(err)
[1] "character"
> class(err)
[1] "character"
> char <- as.numeric(err)
Warning message:
NAs introduced by coercion 
> char
[1]  1 NA  3  4 NA

现在,只是为了(或练习),尝试猜测这些命令的输出:

And now, just for fun (or practice), try to guess the output of these commands:

> fac <- as.factor(err)
> fac
???
> num <- as.numeric(fac)
> num
???

请问Patrick Burns! =)

Kind regards to Patrick Burns! =)

这篇关于如何将数据框列转换为数字类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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