R语言 当某些行是彼此的子集时,如何将Tibble旋转得更宽?

svmlkihl  于 2023-11-14  发布在  其他
关注(0)|答案(2)|浏览(96)

我有类似df1的东西,我想得到df2。我试过使用mutate()pivot_wider(),但我不知道如何表达这个想法。

library(tidyverse)

 df1 <- tibble(
  v1 = c("name1","name1.1","name1.2","name1.3","name2","name2.1","name2.2"),
  v2 = c(9,2,3,4,13,6,7)
)

 df2 <- tibble(
  v1 = c(rep("name1",3),rep("name2",2)),
  v2 = c("name1.1","name1.2","name1.3","name2.1","name2.2"),
  v3 = c(2,3,4,6,7)
)

字符串
注意name1是name1.i的值之和,i = 1,2,3。name2也是一样。

bvjxkvbb

bvjxkvbb1#

无需旋转:

df1 %>%
  mutate(parent = sub("\\..*", "", v1)) %>%
  filter(v1 != parent)
# # A tibble: 5 × 3
#   v1         v2 parent
#   <chr>   <dbl> <chr> 
# 1 name1.1     2 name1 
# 2 name1.2     3 name1 
# 3 name1.3     4 name1 
# 4 name2.1     6 name2 
# 5 name2.2     7 name2

字符串
(尽管名字。

bnl4lu3b

bnl4lu3b2#

您还可以将filterstr_detect和正则表达式一起使用。这里\\w代表任何单词(字母和数字),后跟点\\.,后跟任何数字\\d

library(dplyr)
library(stringr)

df1 |>
  filter(str_detect(v1, "\\w\\.\\d")) |>
  mutate(v3 = str_extract(v1, "\\w+"))

# A tibble: 5 × 3
#  v1         v2   v3
#  <chr>   <dbl> <chr> 
#1 name1.1     2 name1 
#2 name1.2     3 name1 
#3 name1.3     4 name1 
#4 name2.1     6 name2 
#5 name2.2     7 name2

字符串

相关问题