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

Aggregate and reshape from long to wide

Your data are already in a long format that can be used easily by “reshape2”, like this: library(reshape) dcast(df, bdate ~ sex + age + diag, value.var = “admissions”) # bdate Female_35-64_card Female_35-64_cere Female_65-74_card Female_65-74_cere # 1 1987-01-01 1 6 1 6 # 2 1987-01-02 4 4 0 6 # 3 1987-01-03 2 6 4 … Read more

Subsetting R data frame results in mysterious NA rows

Wrap the condition in which: df[which(df$number1 < df$number2), ] How it works: It returns the row numbers where the condition matches (where the condition is TRUE) and subsets the data frame on those rows accordingly. Say that: which(df$number1 < df$number2) returns row numbers 1, 2, 3, 4 and 5. As such, writing: df[which(df$number1 < df$number2), … Read more

Grouped bar plot in ggplot

EDIT: Many years later For a pure ggplot2 + utils::stack() solution, see the answer by @markus! A somewhat verbose tidyverse solution, with all non-base packages explicitly stated so that you know where each function comes from: library(magrittr) # needed for %>% if dplyr is not attached “http://pastebin.com/raw.php?i=L8cEKcxS” %>% utils::read.csv(sep = “,”) %>% tidyr::pivot_longer(cols = c(Food, … Read more

Easy way to convert long to wide format with counts [duplicate]

The aggregation parameter in the dcast function of the reshape2-package defaults to length (= count). In the data.table-package an improved version of the dcastfunction is implemented. So in your case this would be: library(‘reshape2’) # or library(‘data.table’) newdf <- dcast(sample.data, Case ~ Decision) or with using the parameters explicitly: newdf <- dcast(sample.data, Case ~ Decision, … Read more