rtibbler-rownames

The first two columns defined as "rownames"


I want to define the first two columns of a data frame as rownames. Actually I want to do some calculations and the data frame has to be numeric for that.

data.frame <- data_frame(id=c("A1","B2"),name=c("julia","daniel"),BMI=c("20","49"))

The values for BMI are numerical (proved with is.numeric), but the over all data.frame not. How to define the first two columns (id and name) as rownames? Thank you in advance for any suggestions


Solution

  • You can combine id and name column and then assign rownames

    data.frame %>%
      tidyr::unite(rowname, id, name) %>%
      tibble::column_to_rownames()
    
    #          BMI
    #A1_julia   20
    #B2_daniel  49
    

    In base R, you can do the same in steps as

    data.frame <- as.data.frame(data.frame)
    rownames(data.frame) <- paste(data.frame$id, data.frame$name, sep = "_")
    data.frame[c('id', 'name')] <- NULL