Home > Net >  To vector with names from dataframe with rownames
To vector with names from dataframe with rownames

Time:01-26

I have the following vector with names:

myvec <- c(`C1-C` = 3, `C2-C` = 1, `C3-C` = NA, `C4-C` = 5, `C5-C` = NA)

C1-C C2-C C3-C C4-C C5-C 
  3   1   NA   5   NA

I would to convert it in a dadtaframe/tibble... keeping the names of elements as rowname.

The best way that I found it was:

mynames <- names(myvec)
myvec <- myvec %>% 
    as_tibble() %>% 
    mutate(rownames = mynames) %>% 
    column_to_rownames("rownames")

How can I to do this in a more efficient way?

Thanks all

CodePudding user response:

as.data.frame(myvec)

     myvec
C1-C     3
C2-C     1
C3-C    NA
C4-C     5
C5-C    NA

Or

data.frame(myvec)
  • Related