R language data type conversion _r

Source: Internet
Author: User
Tags numeric numeric value

R language Data type conversion

Turn from: http://www.wangluqing.com/2014/09/10/r-share34/

Sometimes, for some problems, the conversion between data types is required. R provides a basic type conversion function to solve the problem of data type conversion. The common basic data type conversion functions are summarized below.

Function One: As.character (x)

Function Two: As.complex (x)

Function Three: As.numeric (x) or as.double (x)

Function Four: As.integer (x)

Function Five: as.logical (x)

Description: The above function indicates that for each basic data type, a function is used to convert the values of other data types to their own data types. If the conversion succeeds, the corresponding result is obtained, and the NA value is obtained. Examples are shown below.

> As.numeric ("3.14")
[1] 3.14
> as.logical (1)
[1] TRUE
> As.character (360)
[1] "360"
> As.complex (1)
[1] 1+0i
> As.numeric ("abc")
[1] NA
Warning message:
NAs introduced by coercion

The above transformation functions can be extended to basic vector types, for example.

> As.character (C (1, 2, 3))
[1] "1" "2" "3"
> As.numeric (C ("1", "2", "3"))
[1] 1 2 3

Note: When a logical value is converted to a numeric value, the value of True is 1,false to 0, for example.

> As.numeric (TRUE)
[1] 1
> as.logical (1)
[1] TRUE
> As.numeric (FALSE)
[1] 0
> as.logical (0)
[1] FALSE

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.