1

Suppose that I have any generic table such as the following:

| Column 1 | Column 2 | Column 3 |
|----------|----------|----------|
| Row 1    |          |          |
| Row 2    |          |          |
| Row 3    |          |          |

How can I pull the row and columns into a vector in R without manually imputing them?

I tried c(table), where tableis the table name, but that didn´t give me the output I expected. I´m looking for the output of a vector containing, c(Row 1, Row 2, Row 3,...), in addition a vector containing c(Column 1, Column 2, Column 3,...).

I saw Converting a dataframe to a vector (by rows), but it creates a vector such as Column 1, Row 1, Column 2, Row 2 or vice versa, while I am looking for two seperate vectors.

nghauran
  • 6,648
  • 2
  • 20
  • 29
MichaelLink
  • 179
  • 11
  • Possible duplicate of [Converting a dataframe to a vector (by rows)](https://stackoverflow.com/questions/2545228/converting-a-dataframe-to-a-vector-by-rows) – nghauran Jun 18 '18 at 07:23
  • @ang I saw that, but I am looking for two separate vectors, one for the column and one for the row. That creates the vector for the column AND the row. – MichaelLink Jun 18 '18 at 07:24
  • 5
    Please provide a real R object and exactly what the output would look like in R. What you're describing could perhaps be done using indexing (`$`, `[`) with a combination of `rownames` or `colnames`. – Roman Luštrik Jun 18 '18 at 07:27
  • 1
    Do you mean a couple of vector with column names and row names? Maybe it is going to fit colnames(data) and rownames(data). – s__ Jun 18 '18 at 07:29

1 Answers1

1

Try this

data(iris)
df <- iris[, 1:4]
head(df) # have a look at df

rows.vec <- as.vector(t(df)) # first vector for rows
cols.vec <- as.vector(unlist(df)) # second vector for columns
nghauran
  • 6,648
  • 2
  • 20
  • 29