如果我们有兴趣在单列中返回 'FullName' 和 'SOCW' 列(重复),select
感兴趣的列,然后使用pivot_longer
withnames_pattern
作为并从不带( ) 后跟数字".value"
的列名称中捕获子字符串。.
[^.]+
library(dplyr)
library(tidyr)
my_data %>%
select(FullName, starts_with("SOCW")) %>%
pivot_longer(cols = starts_with("SOCW"), names_to = ".value",
names_pattern = '^(SOCW[^.]+)')
# A tibble: 6 x 6
FullName SOCW725 SOCW748 SOCW799 SOCW752 SOCW782
<chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Beavis B 3.5 3.22 2.56 3.33 4.2
2 Beavis B 2.33 3.23 NA NA NA
3 Beavis B 3.33 NA NA NA NA
4 El Guapo 3.25 3.02 2.75 4.33 4.15
5 El Guapo 3.33 3.42 NA 4 NA
6 El Guapo 2.67 NA NA NA NA
data.frame
默认情况下不允许重复的列名。它用于通过为每个重复项附加,等make.unique
来修改列名。.1
.2
如果我们只需要三列
library(stringr)
my_data %>%
select(FullName, starts_with("SOCW")) %>%
pivot_longer(cols = starts_with("SOCW")) %>%
mutate(name = str_remove(name, "\\.\\d+$"))
# A tibble: 18 x 3
FullName name value
<chr> <chr> <dbl>
1 Beavis B SOCW725 3.5
2 Beavis B SOCW748 3.22
3 Beavis B SOCW799 2.56
4 Beavis B SOCW725 2.33
5 Beavis B SOCW752 3.33
6 Beavis B SOCW782 4.2
7 Beavis B SOCW725 3.33
8 Beavis B SOCW748 3.23
9 Beavis B SOCW752 NA
10 El Guapo SOCW725 3.25
11 El Guapo SOCW748 3.02
12 El Guapo SOCW799 2.75
13 El Guapo SOCW725 3.33
14 El Guapo SOCW752 4.33
15 El Guapo SOCW782 4.15
16 El Guapo SOCW725 2.67
17 El Guapo SOCW748 3.42
18 El Guapo SOCW752 4
数据
my_data <- structure(list(FullName = c("Beavis B", "El Guapo"), SOCW725 = c(3.5,
3.25), SOCW748 = c(3.22, 3.02), SOCW799 = c(2.56, 2.75), Average = c(3.07,
3.18), SOCW725.1 = c(2.33, 3.33), SOCW752 = c(3.33, 4.33), SOCW782 = c(4.2,
4.15), Average.1 = c(3.5, 2.25), SOCW725.2 = c(3.33, 2.67), SOCW748.1 = c(3.23,
3.42), SOCW752.1 = c(NA, 4L), Average.2 = c(3, 2.44)),
class = "data.frame", row.names = c(NA,
-2L))