Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Eliminating row.names from a data.frame

I have a data frame named AE, typically I set the row names to NULL to allow an rbind. In this cases when I do that, the row.names are still "1","2", and do not allow a bind. I am not understanding what is different about this data.frame.

#load Data    
AE <- structure(list(mean = c(0.363510076570372, 0.636489923429628), 
        SE = c(0.00728114835455055, 0.00728114835455055), grp = c("D", 
        "DP"), level = c("fair or poor health", 
        "good or better health"), counts = structure(list(counts = c(25405L, 
        25405L)), row.names = c("counts", "counts1"), class = "data.frame")), row.names = c(NA, 
    -2L), class = "data.frame")
    
#remove rownames
    rownames( AE ) <- NULL

#this is the line i want to work
    rbind( AE, AE)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The main reason is that one of the column is a data.frame.

> str(AE)
'data.frame':   2 obs. of  5 variables:
 $ mean  : num  0.364 0.636
 $ SE    : num  0.00728 0.00728
 $ grp   : chr  "D" "DP"
 $ level : chr  "fair or poor health" "good or better health"
 $ counts:'data.frame': 2 obs. of  1 variable: ###
  ..$ counts: int  25405 25405

We may need to convert it to a regular column

AE <- do.call(data.frame, AE)
out <- rbind(AE, AE)
row.names(out) <- NULL
> out
       mean          SE grp                 level counts
1 0.3635101 0.007281148   D   fair or poor health  25405
2 0.6364899 0.007281148  DP good or better health  25405
3 0.3635101 0.007281148   D   fair or poor health  25405
4 0.6364899 0.007281148  DP good or better health  25405

Or using tidyverse, unpack to convert to a regular column and then use bind_rows

library(tidyr)
library(dplyr)
unpack(AE, counts) %>% 
   bind_rows(., .)
# A tibble: 4 × 5
   mean      SE grp   level                 counts
  <dbl>   <dbl> <chr> <chr>                  <int>
1 0.364 0.00728 D     fair or poor health    25405
2 0.636 0.00728 DP    good or better health  25405
3 0.364 0.00728 D     fair or poor health    25405
4 0.636 0.00728 DP    good or better health  25405
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading