R语言 基于单元格内容透视到宽窗体

tkclm6bt  于 12个月前  发布在  其他
关注(0)|答案(3)|浏览(98)

我有一个大的数据表,其中某些参数被测量,
1.没有特定的一面(我称之为“NoSide”),
1.左右两边都有。

myinput <- data.frame(Parameter=letters[1:10], NoSide=c(NA,NA,NA,1,3,3,2,0,1,NA), Right=c(2,3,1,NA,NA,NA,NA,NA,NA,1), Left=c(0,1,4,NA,NA,NA,NA,NA,NA,2))

> myinput
   Parameter NoSide Right Left
1          a     NA     2    0
2          b     NA     3    1
3          c     NA     1    4
4          d      1    NA   NA
5          e      3    NA   NA
6          f      3    NA   NA
7          g      2    NA   NA
8          h      0    NA   NA
9          i      1    NA   NA
10         j     NA     1    2

字符串
我希望数据以宽格式显示如下,因为在这种特定情况下,右侧和左侧的测量值是离散值,不应该集中在一起:

myheaders = c("a_Right","a_Left","b_Right","b_Left","c_Right","c_Left","d","e","f","g","h","i","j_Right","j_Left")
mydata = c(2,0,3,1,1,4,1,3,3,2,0,1,1,2)
myoutput = as.data.frame(t(mydata))
colnames(myoutput) <- myheaders
myoutput

> myoutput
  a_Right a_Left b_Right b_Left c_Right c_Left d e f g h i j_Right j_Left
1       2      0       3      1       1      4 1 3 3 2 0 1       1      2


从本质上讲,NoSide、Right和Left列的内容是从左到右读取的,并以宽格式列出,同时省略“NA”值。应根据值是从“NoSide”、“Right”还是“Left”列派生的(理想情况下,名称中没有“NoSide”)创建新列名。
您有没有关于最简单的方式转换myinputmyoutput的任何建议?谢谢!

zqdjd7g9

zqdjd7g91#

pivot_wider(myinput, 
 names_from = Parameter, values_from = NoSide:Left, names_vary='slowest',
 names_glue = "{Parameter}{if_else(.value=='NoSide','',str_c('_',.value))}") %>% 
select(!where(is.na))

# A tibble: 1 × 14
  a_Right a_Left b_Right b_Left c_Right c_Left     d     e     f     g     h     i j_Right j_Left
    <dbl>  <dbl>   <dbl>  <dbl>   <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>   <dbl>  <dbl>
1       2      0       3      1       1      4     1     3     3     2     0     1       1      2

字符串

wlp8pajw

wlp8pajw2#

虽然我不明白为什么你要这样重塑数据,但你可以通过首先重塑为wide,然后去掉NA列,最后重新排序和重命名来实现你想要的结果:

myinput <- data.frame(
  Parameter = letters[1:10],
  NoSide = c(NA, NA, NA, 1, 3, 3, 2, 0, 1, NA),
  Right = c(2, 3, 1, NA, NA, NA, NA, NA, NA, 1),
  Left = c(0, 1, 4, NA, NA, NA, NA, NA, NA, 2)
)

library(tidyr)
library(dplyr, warn = FALSE)

myinput %>%
  tidyr::pivot_wider(
    names_from = Parameter,
    values_from = -Parameter,
    names_glue = "{Parameter}_{.value}"
  ) %>%
  dplyr::select(!where(~ all(is.na(.x)))) %>%
  dplyr::select(sort(names(.))) |>
  dplyr::rename_with(
    ~ gsub("_NoSide", "", .x),
    ends_with("NoSide")
  )
#> # A tibble: 1 × 14
#>   a_Left a_Right b_Left b_Right c_Left c_Right     d     e     f     g     h
#>    <dbl>   <dbl>  <dbl>   <dbl>  <dbl>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1      0       2      1       3      4       1     1     3     3     2     0
#> # ℹ 3 more variables: i <dbl>, j_Left <dbl>, j_Right <dbl>

字符串

mbyulnm0

mbyulnm03#

这是另一个版本:

library(dplyr)
library(tidyr)

myinput %>% 
  pivot_longer(cols = c(NoSide, Right, Left), names_to = "Side", values_drop_na = TRUE) %>%
  mutate(Parameter = if_else(Side == "NoSide", as.character(Parameter), paste(Parameter, Side, sep = "_"))) %>%
  select(-Side) %>%
  pivot_wider(names_from = Parameter, values_from = value)

 a_Right a_Left b_Right b_Left c_Right c_Left     d     e     f     g     h     i j_Right j_Left
    <dbl>  <dbl>   <dbl>  <dbl>   <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>   <dbl>  <dbl>
1       2      0       3      1       1      4     1     3     3     2     0     1       1      2

字符串

相关问题