excel 在R中将长数据整形/重复数据消除为宽数据[重复]

sauutmhj  于 2023-02-25  发布在  其他
关注(0)|答案(1)|浏览(189)
    • 此问题在此处已有答案**:

How to reshape data from long to wide format(14个答案)
6小时前关门了。
我一直在尝试在excel中用ID将这些长数据转换成宽格式,但没有成功,我尝试使用dcast,但它没有给我预期的结果。
我已经附加了一个csv格式的数据是如何目前在单元格:(a1:c10),以及细胞:(f1:n4)是我希望它被格式化的方式。我先在excel中尝试过,但没有使用power query的经验,我想也许reforme2或dcast可以做类似的事情。

在r中我做到了:

olddata_wide$ID <- factor(olddata_wide$ID)

widedf <- dcast(df1, ID  ~ paydate, value.var="Type")

这只是给了我一个日期输出。

xfb7svmp

xfb7svmp1#

使用pivot_widerrename

library(dplyr)
library(tidyr)

repl <- c("1st_transaction" = "type_1", "2nd_transaction" = "type_2", 
  "3rd_transaction" = "type_3", "4th_transaction" = "type_4")

df %>% 
  mutate(n = row_number(), .by = ID) %>% 
  pivot_wider(names_from = n, values_from = c(type, paydate)) %>% 
  rename(all_of(repl))
# A tibble: 3 × 9
  ID     1st_transacti…¹ 2nd_t…² 3rd_t…³ 4th_t…⁴ payda…⁵ payda…⁶ payda…⁷ payda…⁸
  <chr>  <chr>           <chr>   <chr>   <chr>   <chr>   <chr>   <chr>   <chr>  
1 AAL100 H               H       B       NA      8/28/2… 8/28/2… 8/28/2… NA     
2 AAC926 H               H       NA      NA      8/28/2… 8/28/2… NA      NA     
3 ABR765 V               H       H       B       8/17/2… 8/28/2… 8/28/2… 8/28/2…
# … with abbreviated variable names ¹​`1st_transaction`, ²​`2nd_transaction`,
#   ³​`3rd_transaction`, ⁴​`4th_transaction`, ⁵​paydate_1, ⁶​paydate_2, ⁷​paydate_3,
#   ⁸​paydate_4
数据
df <- structure(list(ID = c("AAL100", "AAL100", "AAL100", "AAC926", 
"AAC926", "ABR765", "ABR765", "ABR765", "ABR765"), paydate = c("8/28/2019", 
"8/28/2020", "8/28/2021", "8/28/2017", "8/28/2018", "8/17/2016", 
"8/28/2020", "8/28/2021", "8/28/2022"), type = c("H", "H", "B", 
"H", "H", "V", "H", "H", "B")), class = "data.frame", row.names = c(NA, 
-9L))

相关问题