创建具有重复行的数据框

Creating data frame with repeat rows

我想创建一个包含重复行的数据框。

这是我的原始数据集:

> mtcars_columns_a
  variables_interest data_set data_set_and_variables_interest      mean
1                mpg   mtcars                      mtcars$mpg  20.09062
2               disp   mtcars                     mtcars$disp 230.72188
3                 hp   mtcars                       mtcars$hp 146.68750

这是我想要的数据集

> mtcars_columns_b
  variables_interest data_set data_set_and_variables_interest      mean
1                mpg   mtcars                      mtcars$mpg  20.09062
2                mpg   mtcars                      mtcars$mpg  20.09062
3               disp   mtcars                     mtcars$disp 230.72188
4               disp   mtcars                     mtcars$disp 230.72188
5                 hp   mtcars                       mtcars$hp 146.68750
6                 hp   mtcars                       mtcars$hp 146.68750

我知道如何手动完成这项工作,但这既费时又死板。有没有更快、更自动化、更灵活的方法?



这是我用来创建数据集的代码:

# mtcars data

## displays data
mtcars

## 3 row data set

### lists columns of interest
# ---- NOTE: REQUIRES MANUAL INPUT
# ---- NOTE: lists variables of interest
mtcars_columns_a <- 
  data.frame(
    c(
      "mpg",
      "disp",
      "hp"
    )
  )
# ---- NOTE: REQUIRES MANUAL INPUT
# ---- NOTE: adds colnames
names(mtcars_columns_a)[names(mtcars_columns_a) == 'c..mpg....disp....hp..'] <- 'variables_interest'

### adds data set info
mtcars_columns_a$data_set <- 
  c("mtcars")

### creates data_set_and_variables_interest column
mtcars_columns_a$data_set_and_variables_interest <- 
  paste(mtcars_columns_a$data_set,mtcars_columns_a$variables_interest,sep = "$")

### creates mean column
mtcars_columns_a$mean <-
  c(
    mean(mtcars$mpg),
    mean(mtcars$disp),
    mean(mtcars$hp)
  )

## 6 row data set., the long way

### lists columns of interest
# ---- NOTE: REQUIRES MANUAL INPUT
# ---- NOTE: lists variables of interest
mtcars_columns_b <- 
  data.frame(
    c(
      "mpg",
      "mpg",
      "disp",
      "disp",
      "hp",
      "hp"
    )
  )
# ---- NOTE: REQUIRES MANUAL INPUT
# ---- NOTE: adds colnames
names(mtcars_columns_b)[names(mtcars_columns_b) == 'c..mpg....mpg....disp....disp....hp....hp..'] <- 'variables_interest'

### adds data set info
mtcars_columns_b$data_set <- 
  c("mtcars")

### creates data_set_and_variables_interest column
mtcars_columns_b$data_set_and_variables_interest <- 
  paste(mtcars_columns_b$data_set,mtcars_columns_b$variables_interest,sep = "$")

### creates mean column
mtcars_columns_b$mean <-
  c(
    mean(mtcars$mpg),
    mean(mtcars$mpg),
    mean(mtcars$disp),
    mean(mtcars$disp),
    mean(mtcars$hp),
    mean(mtcars$hp)
  )

根据您的预期输出,这是您想要的吗?

使用 select 函数选择所需变量,并使用 group_by 个变量后的 summarise 函数计算平均值。

使用 mutate 执行数据复制和添加附加变量(不确定是否有必要)。

您可以使用 dplyr::rename 函数编辑变量名称。

library(dplyr)
library(tidyr)


df <- 
  mtcars %>% 
  select(mpg, disp, hp) %>% 
  pivot_longer(everything()) %>% 
  group_by(name) %>% 
  summarise(mean = mean(value))

df1 <- 
  bind_rows(df, df) %>% 
  arrange(name) %>% 
  mutate(dataset = "mtcars",
         variable = paste(dataset, name, sep = "$"))

df1
#> # A tibble: 6 x 4
#>   name   mean dataset variable   
#>   <chr> <dbl> <chr>   <chr>      
#> 1 disp  231.  mtcars  mtcars$disp
#> 2 disp  231.  mtcars  mtcars$disp
#> 3 hp    147.  mtcars  mtcars$hp  
#> 4 hp    147.  mtcars  mtcars$hp  
#> 5 mpg    20.1 mtcars  mtcars$mpg 
#> 6 mpg    20.1 mtcars  mtcars$mpg

reprex package (v1.0.0)

于 2021-04-06 创建

data.frame 对象中的记录顺序通常没有意义,因此您可以这样做:

rbind(mtcars_columns_a, mtcars_columns_a)

如果你需要按照你展示的顺序,这个也简单:

mtcars_columns_b <- rbind(mtcars_columns_a, mtcars_columns_a)
mtcars_columns_b[order(mtcars_columns_b, mtcars_columns_b$name),]

您可以像下面那样尝试rep

mtcars_columns_a[rep(seq(nrow(mtcars_columns_a)), each = 2),]

另一种选择是uncount

library(dplyr)
library(tidyr)
mtcars_columns_a %>%
   uncount(2)