R: Reshaping Multiple Columns from Long to Wide

An option would be to replace the duplicated elements by ‘Letter’ to NA and then in the reshaped data, remove the columns that are all NA library(data.table) out <- dcast(setDT(sample_df)[, lapply(.SD, function(x) replace(x, duplicated(x), NA)), Letter], Letter ~ rowid(Letter), value.var = c(“Number”, “Fruit”)) nm1 <- out[, names(which(!colSums(!is.na(.SD))))] out[, (nm1) := NULL][] # Letter Number_1 Number_2 … Read more