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