Search code examples
rdataframevector

R Convert all values of multiple dataframe rows to one vector in order


I have a dataframe in R with observations that I would like to convert to one vector. All the rows have the same columns (with some NAs present in certain columns). I know that I can extract them all individually, but I am wondering what the most succinct way to extract all of the values (in order) is, to then convert them to one vector. I would like to read out all the values of row 1 (column-1, column-2... column-n), followed by row 2, followed by row 3...etc.

Example with sample data:

Row 1: 1, 2, 3, 4, 5, NA, 1

Row 2: 2, 3, 4, 5, 6, 8, NA

Row 3: 7, 9, 2, 3, 5, 12, 14

Desired Vector: 1, 2, 3, 4, 5, NA, 1, 2, 3, 4, 5, 6, 8, NA, 7, 9, 2, 3, 5, 12, 14

To then remove NAs and receive:
1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6, 8, 7, 9, 2, 3, 5, 12, 14

Given that I have numerous rows and columns, what is the most succinct way to do this, rather than individually extracting each row/column's values?


Solution

  • You can also do this:

    df |>
      #turn the rows into columns
      t() |>
      #combine the colums into one vector
      Reduce(f = c) |>
      #drop na
      Filter(f = Negate(is.na))
    # [1]  1  2  3  4  5  1  2  3  4  5  6  8  7  9  2  3  5 12 14
    

    The df is from Mohamed Desouky.

    You can also use apply with the MARGIN=2 argument and drop the NA first. Then combine the list into a sigle vector:

    df |> apply(2, na.omit) |> Reduce(f = c)
    # x  y  z  x  y  z  x  y  z  x  y  z  x  y  z  y  z  x  z 
    # 1  2  7  2  3  9  3  4  2  4  5  3  5  6  5  8 12  1 14