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
Thanks for posting.
You're very welcome! :)
Hii, is it possible to get pivot_wider result based on alphabetic order
Hello Manik,
What do you like to order alphabetically? I didn't get that part well.
Regards,
Cansu
@@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?
@@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
@@cansustatisticsglobe wow, cool... thank you so much for your effort...this is what I needed... thank you again
@@MrManikprabhu Welcome!