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.

4
  • Possible duplicate of Converting a dataframe to a vector (by rows) Commented Jun 18, 2018 at 7: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. Commented Jun 18, 2018 at 7: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. Commented Jun 18, 2018 at 7: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). Commented Jun 18, 2018 at 7:29

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.