Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
464 views
in Technique[技术] by (71.8m points)

r - 如何将数据框列转换为数字类型?(How to convert a data frame column to numeric type?)

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

  ask by acroa translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 .

(由于(仍然)没有人打勾,因此我认为您会遇到一些实际问题,主要是因为您尚未指定要将哪种类型的向量转换为numeric 。)

I suggest that you should apply transform function in order to complete your task.

(我建议您应该应用transform功能以完成任务。)

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)

Let us have a glance at data.frame

(让我们看一下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

and let us run:

(让我们运行:)

> 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" 

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.

(嗯,我碰到了R中相当独特的东西,而这还不是)最混杂的东西,但它可以迷惑你,特别是如果你滚进睡前阅读。)

Here goes: first two columns are character .

(往前走:前两列是character 。)

I've deliberately called 2 nd one fake_char .

(我特意将第二fake_char 。)

Spot the similarity of this character variable with one that Dirk created in his reply.

(发现此character变量与Dirk在他的回复中创建的变量相似。)

It's actually a numerical vector converted to character .

(它实际上是一个转换为characternumerical矢量。)

3 rd and 4 th column are factor , and the last one is "purely" numeric .

(3 4列是factor ,最后一个是“纯粹的” numeric 。)

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

(如果您使用transform功能,则可以将fake_char转换为numeric ,但不能转换为char变量本身。)

> 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

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

(但是,如果您对fake_charchar_fac做同样的事情,您会很幸运,并且没有NA:)

> 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

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

(如果保存转换后的data.frame并检查modeclass ,则会得到:)

> 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"

So, the conclusion is: Yes, you can convert character vector into a numeric one, but only if it's elements are "convertible" to numeric . (因此,得出的结论是: 是的,您可以将character向量转换为numeric ,但)前提是它的元素可以“转换”为numeric))

If there's just one character element in vector, you'll get error when trying to convert that vector to numerical one.

(如果向量中只有一个character元素,则尝试将该向量转换为numerical时会出错。)

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
???

Kind regards to Patrick Burns!

(问候帕特里克·伯恩斯!)

=)

(=))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...