Spread with duplicate identifiers (using tidyverse and %>%) [duplicate]

We can use tidyverse. After grouping by ‘start_end’, ‘id’, create a sequence column ‘ind’ , then spread from ‘long’ to ‘wide’ format library(dplyr) library(tidyr) df %>% group_by(start_end, id) %>% mutate(ind = row_number()) %>% spread(start_end, date) %>% select(start, end) # id start end #* <int> <fctr> <fctr> #1 2 1994-05-01 1996-11-04 #2 4 1979-07-18 NA #3 … Read more

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

combine two data frames with all posible combinations

With this particular example I think you can just use the merge function. As a standard its arguments all.x and all.y are set to TRUE, so it automatically creates all combinations since the dataframes do not have any variables or values in common. df <-data.frame(a=letters[1:10] ) df1<-data.frame(one=1:10) dfcomb <- merge(df,df1) dim(dfcomb) [1] 100 2 #gives … Read more