tidyr包

library(tidyr)
library(dplyr)
data(who)
names(who)

#pivot_longer类似gather
who %>% pivot_longer(
  cols = new_sp_m014:newrel_f65,
  names_to = c("diagnosis", "gender", "age"),
  names_pattern = "new_?(.*)_(.)(.*)",
  values_to = "count",
  values_drop_na = TRUE
)

stocks <- tibble(
  time = as.Date('2009-01-01') + 0:9,
  X = rnorm(10, 0, 1),
  Y = rnorm(10, 0, 2),
  Z = rnorm(10, 0, 4)
)
stocks %>% gather("stock", "price", -time)
stocks %>% pivot_longer(-time,
                        names_to = 'stock',
                        values_to = 'price')

stocksm <- stocks %>%
  pivot_longer(cols = c(X,Y,Z),
               names_to = 'stock',
               values_to = 'price')

stocksm %>% spread(stock, price)
#填补缺失值
stocksm %>% pivot_wider(names_from = stock,
                        values_from = price,
                        values_fill = list(seen = 0))
#每行有4个配对的x y值,使用特殊 .value
data(anscombe)
pivot_ans <- anscombe %>%
  pivot_longer(
    everything(),
    names_to = c(".value", "set"),
    names_pattern = "(.)(.)"
  ) %>%
  as_tibble()

############################将嵌套列表整形
characters <- list(
  list(
    name = "Theon Greyjoy",
    aliases = c("Prince of Fools", "Theon Turncloak", "Theon Kinslayer"),
    alive = TRUE
  ),
  list(
    name = "Tyrion Lannister",
    aliases = c("The Imp", "Halfman", "Giant of Lannister"),
    alive = TRUE
  ),
  list(
    name = "Arys Oakheart",
    alive = FALSE
  )
)
# 创建dataframe, list-column
got <- tibble(character = characters)
# use unnest_wider() 使得list转为column
got %>%
  unnest_wider(character)
# unnest_longer() 使得aliases list转为每一行
got %>%
  unnest_wider(character) %>%
  unnest_longer(aliases)
#unnest_auto()自动猜测功能
got %>%
  unnest_auto(character) %>%
  unnest_auto(aliases)
#使用hoist()深入到数据结构中并只输出你需要的部分:
got %>% hoist(character,
              name = "name",
              alias = list("aliases", 1),
              alive = "alive"
)
#这种语法比在dplyr :: mutate()中使用purrr :: map()提供了更平易近人的替代方法:
got %>% mutate(
  name = purrr::map_chr(character, "name"),
  alias = purrr::map_chr(character, list("aliases", 1), .default = NA),
  alive = purrr::map_lgl(character, "alive"))
 
###############################嵌套
df <- tibble(x = c(1, 1, 1, 2, 2, 3), y = 1:6, z = 6:1)
df %>% nest(data = c(x, y, z))
df %>% unnest(c(x, y, z))

#######################填充
expand_grid(
  x = 1:3,
  y = letters[1:3],
  z = LETTERS[1:3]
)
#the first element 变化最慢
students <- tribble(
  ~ school, ~ student,
  "A",      "John",
  "A",      "Mary",
  "A",      "Susan",
  "B",      "John"
)
expand_grid(students, semester = 1:2)

评论

此博客中的热门博文

V2ray websocket(ws)+tls+nginx分流

Rstudio 使用代理