this is probably something stupid I'm doing but I'm trying to join the values of some columns together based on whether they contain a value or not, and then create two new columns called start.week and end.week.
My start.week is "Monday, Tuesday and Wednesday". The end.week is "Thursday and Friday".
Name Monday Tuesday Wednesday Thursday Friday
John Red Pink
Francis Blue Gray Black
Bill Green Orange Purple
Bob Yellow Lilac Magenta
I can add the two columns together for Thursday and Friday using:
start.week = c("Monday", "Tuesday", "Wednesday")
end.week = c("Thursday", "Friday")
options(stringsAsFactors = FALSE)
df = mutate(df, end.week = ifelse(Friday != "", paste0(Thursday, " + ", Friday), Thursday))
Back I can't work out how to to this for start.week
Can anyone please give me a hint? I'd be forever grateful.
Original data:
df = structure(list(Name = c("John", "Francis", "Bill", "Bob"), Monday =
c("Red", "Blue", "Green", "Yellow"), Tuesday = c("", "Gray", "", ""),
Wednesday = c("Pink", "", "Orange", ""), Thursday = c("",
"Black", "Purple", "Lilac"), Friday = c("", "", "", "Magenta"
)), class = "data.frame", row.names = c(NA, -4L))
Expected Output:
df = structure(list(Name = c("John", "Francis", "Bill", "Bob"), Monday =
c("Red", "Blue", "Green", "Yellow"), Tuesday = c("", "Gray", "", ""),
Wednesday = c("Pink", "", "Orange", ""), Thursday = c("",
"Black", "Purple", "Lilac"), Friday = c("", "", "", "Magenta"
), start.week = c("Red + Pink", "Black", "Green + Orange",
"Yellow"), end.week = c("", "", "Purple", "Lilac + Magenta"
)), class = "data.frame", row.names = c(NA, -4L))
start.week
should instead be"Blue + Gray"
, is that right? – r2evans