从data.frame中消除行名称

qjp7pelc  于 2023-03-27  发布在  其他
关注(0)|答案(1)|浏览(155)

我有一个名为AE的 Dataframe ,通常我将行名称设置为NULL以允许rbind。在这种情况下,当我这样做时,行.名称仍然是“1”,“2”,并且不允许绑定。我不明白这个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)
vfh0ocws

vfh0ocws1#

主要原因是其中一列是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

我们可能需要将其转换为常规列

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

或者使用tidyverseunpack转换为常规列,然后使用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

相关问题