pivot_longer & pivot_wider Functions of tidyr Package in R | Reshape Data from Wide to Long Format

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 พ.ย. 2024
  • How to apply the pivot_longer and pivot_wider functions of the tidyr add-on package in the R programming language. More details: statisticsglob...
    R code of this video:
    data <- data.frame(ID1 = LETTERS[1:4], # Create example data
    ID2 = rep(letters[1:3], each = 4),
    x = 1:12,
    y = 21:32)
    install.packages("tidyr") # Install tidyr package
    library("tidyr") # Load tidyr package
    data_long <- pivot_longer(data = data, # Convert wide to long
    cols = c("x", "y"))
    data_wide <- pivot_wider(data = data_long) # Convert long to wide
    Follow me on Social Media:
    Facebook - Statistics Globe Page: / statisticsglobecom
    Facebook - Group for Discussions & Questions: / statisticsglobe
    LinkedIn - Statistics Globe Page: / statisticsglobe
    LinkedIn - Group for Discussions & Questions: / 12555223
    Twitter: / joachimschork
    Music by bensound.com

ความคิดเห็น • 8

  • @theecanmole
    @theecanmole 2 ปีที่แล้ว +3

    Thanks for posting.

  • @MrManikprabhu
    @MrManikprabhu ปีที่แล้ว +2

    Hii, is it possible to get pivot_wider result based on alphabetic order

    • @cansustatisticsglobe
      @cansustatisticsglobe ปีที่แล้ว

      Hello Manik,
      What do you like to order alphabetically? I didn't get that part well.
      Regards,
      Cansu

    • @MrManikprabhu
      @MrManikprabhu ปีที่แล้ว +1

      @@cansustatisticsglobe like i have a long table (3 columns (product1, product 2, and value of product 1 and 2) and 20 rows) when I do pivot_wider (names_from = "product1", values_from = "value"), I get the output but product names from product1 are not arranged alphabetically. Is it possible to arrange it?

    • @cansustatisticsglobe
      @cansustatisticsglobe ปีที่แล้ว

      @@MrManikprabhu If I got you correct, the names_sort argument would help you with what you want. I adapted the code to illustrate your case; see below:
      data

    • @MrManikprabhu
      @MrManikprabhu ปีที่แล้ว +1

      @@cansustatisticsglobe wow, cool... thank you so much for your effort...this is what I needed... thank you again

    • @cansustatisticsglobe
      @cansustatisticsglobe ปีที่แล้ว

      @@MrManikprabhu Welcome!