Search code examples
rdataframerbindcbind

R dataframe create


I have a data frame in R, say f1. I want to create another data frame f2 which has the column names(header) in f1 as f2's row names. I know there are 300 names in f1, and want to assign color "#ff0000" to the first 200 and color "#0000ff" to the last 100. How can I do this? The result should looks like,

name1   "#ff0000"
name2   "#ff0000"
...
name201 "#0000ff"
name202 "#0000ff"
...

Solution

  • The rbinds and cbinds in your answer are unnecessary. This is a one-liner, using data.frame.

    f2 = data.frame(color = c(rep("#ff0000", 200), rep("#0000ff", 100)),
        row.names = names(f1),
        stringsAsFactors = FALSE)