Open In App

Pivot data from long to wide format using R

Last Updated : 25 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will explore various methods to convert the pivot data from long to wide format by using R Programming Language.

How to convert the pivot data from long to wide format

R language offers various methods to convert the pivot data from long to wide format. By using these methods, can work more efficiently. Some of the methods to convert pivot data from long to wide format are:

By using the reshape() function

This method is used to convert the pivot data from long to wide format efficiently. In the below example, we created a data frame and converted the pivot data from long to wide format.

R
ID = rep(1:2, each = 3)
  Time = rep(1:3, 2)
  Values = c(10:15)
  
# creating data frame
data <- data.frame(ID, Time, Values)

print("Long format of pivot data")
print(data)

wide_data <- reshape(data, idvar = "ID", timevar = "Time", direction = "wide")
rownames(wide_data) <- NULL  # Remove row names

print("Wide format of pivot data")
print(wide_data)

Output:

[1] "Long format of pivot data"
    ID   Time   Values
1    1       1          10
2   1       2          11
3   1       3         12
4   2      1          13
5   2      2         14
6   2      3         15

[1] "Wide format of pivot data"
   ID   Values.1   Values.2   Values.3
1   1       10             11               12
2  2       13            14               15

In the below example, we created a data frame and converted the pivot data from long to wide format.

R
# creating a dataframe
data <- data.frame(
  Student = c("A", "B", "C", "A", "B", "C"),
  Subject = c("Physics", "Science", "Physics", "Science", "Physics", "Science"),
  Score = c(80, 75, 85, 90, 85, 88)
)
print("The long-format of pivot data")
print(data)

wide_data <- reshape(data, idvar = "Student", timevar = "Subject", direction = "wide")

print("Wide-format of pivot data")
print(wide_data)

Output:

[1] "The long-format of pivot data"
    Student    Subject     Score
1       A            Physics       80
2      B            Science      75
3      C            Physics       85
4      A            Science      90
5      B           Physics        85
6      C           Science       88

[1] "Wide-format of pivot data"
     Student   Score.Physics   Score.Science
1       A                   80                     90
2       B                  85                     75
3       C                  85                     88

By using the base R functions

This method is used to convert the pivot data from long to wide format efficiently. In the below example, we created a data frame and converted the pivot data from long to wide format.

R
rno = rep(1:2, each = 4)
Time = rep(1:4, 2)
Values = c(10, 20, 30, 40, 50, 60,70,80)

# creating data frame
df<- data.frame(rno,Time,Values)

print("Long-format of pivot data")
print(df)

# Get unique Time values
times <- unique(df$Time)

wide_data <- data.frame(rno = unique(df$rno))

for (t in times) {

  values <- df$Value[df$Time == t]
  
  wide_data <- cbind(wide_data, values)
}
# Renaming of columns
colnames(wide_data)[-1] <- paste0("Time", times)

print("Wide formmat of pivot data")
print(wide_data)

Output:

   [1] "Long-format of pivot data"
    rno Time   Values
1   1        1          10
2   1       2         20
3   1       3         30
4   1      4          40
5   2      1          50
6   2      2         60
7   2      3          70
8   2      4          80

[1] "Wide formmat of pivot data"
    rno   Time1   Time2   Time3   Time4
1   1          10         20         30          40
2   2        50        60          70          80

In the below example, we created a data frame and converted the pivot data from long to wide format.

R
# creating data frame
df = data.frame(
  ID = c(2, 2, 6, 6),
  name = c("C", "D", "C", "D"),
  Value = c(10, 20, 30, 40)
)
print("Long format of pivot data")
print(df)

#  To identify the unique IDs
unique_ids <- unique(df$ID)

wide_data <- data.frame(ID = unique_ids)

for (id in unique_ids) {
  # Subset data for each ID
  subset_data <- subset(df, ID == id)
  
  for (time_value in unique(subset_data$name)) {
  
    value <- subset(subset_data, name == time_value)$Value
    
    # Assign value to the wide_data frame
wide_data[wide_data$ID == id, as.character(time_value)] = value
  }
}
print("wide format of pivot data")
print(wide_data)

Output:

[1] "Long format of pivot data"
    ID  name  Value
1    2      C          10
2   2      D          20
3   6      C          30
4   6      D         40

[1] "wide format of pivot data"
   ID   C   D
1   2   10  20
2  6   30  40

Conclusion

In Conclusion, we learned about how to convert the pivot data from long to wide format by using R. R language offers versatile tools while converting the pivot data from long to wide format.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads