Data concatenation and coercion are common operations in R.

*Data Concatenation*

*Data Concatenation*

The concatenate c() function is used to combine elements into a vector.

1 2 3 4 5 |
> c(T, F, T) [1] T F T > c(8.3, 9.2, 11) [1] 8.3 9.2 11.0 |

When elements are combined from different classes, the c() function coerces to a common type, which is the type of the returned value:

1 2 3 4 |
> x <- c(100, "A", TRUE, as(1, "complex")) > x [1] "100" "A" "TRUE" "1+0i" > class(x) [1] "character" |

*Paste Functions in R*

*Paste Functions in R*

The paste0() and the paste() functions are used to concatenate strings and vectors after converting to character. Both are used extensively to define frequently used character strings, such as file paths, variable names, and strings generated by iterative functions. The first function assumes the string elements to be combined have no separator (e.g. sep=””), while the second paste function requires a used defined separator:

1 2 3 4 5 |
> paste0("A", 1:6) [1] "A1" "A2" "A3" "A4" "A5" "A6" > paste(“C:”, “UserName”, “projectX”, sep=”/”) [1] "C:/Username/projectX" |

*Data Coercion in R*

*Data Coercion in R*

If elements of different modes are combined, R implicitly coerces all elements to the most informative class type. As a rule, TRUE become 1 and FALSE becomes 0 when logical values are coerced to integers. Some of the results below may seem non-sensical, but the examples clearly illustrate how data elements can be coerced.

1 2 3 4 5 6 7 8 9 10 11 |
> x <- c(T, 8.3, 5) > class(x) [1] "numeric" > y <- c(T, 8.3, "hello") > class(y) [1] "character" > z <- c(8.3, 9+6i) > class(z) [1] "complex" |

Coercion is subject to testing and control. The function is(x, “type”) is used to test if x is a certain type of class. The function as(x, “type”) explicitly coerces data to a certain type.

Class | Test | Coerce |
---|---|---|

array | is(x, "array") | as(x, "array") |

character | is(x, "character") | as(x, "character") |

complex | is(x, "complex") | as(x, "complex") |

data.frame | is(x, "data.frame") | as(x, "data.frame") |

double | is(x, "double") | as(x, "double") |

environment | is(x, "environment") | as(x, "environment") |

factor | is(x, "factor") | as(x, "factor") |

integer | is(x, "interger") | as(x, "interger") |

list | is(x, "list") | as(x, "list") |

logical | is(x, "logical") | as(x, "logical") |

matrix | is(x, "matrix") | as(x, "matrix") |

NA | is(x, "na") | as(x, "na") |

NaN | is(x, "nan") | is(x, "nan") |

NULL | is(x, "NULL") | as(x, "NULL") |

numeric | is(x, "numeric") | as(x, "numeric") |

single | is(x, "single") | as(x, "single") |

timeDate | is(x, "timeDate") | as(x, "timeDate") |

timeSeries | is(x, "timeSeries") | as(x, "timeSeries") |

vector | is(x, "vector") | as(x, "vector") |