how to use dcast on a dataframe that has only 1 row in R -
hi have dataframe
start <- c("a") end <- c("c") days <- c("day1") df2 <- data.frame(start,end,days)
i trying use dcast
df2 <- dcast(df2,days ~ end,value.var="days")
but returns
days c 1 day1 day1
my desired output count
days c 1 day1 1
what missing here? kindly provide inputs on this. there better way using dplyr?
we can create sequence column of 1 , use dcast
dcast(transform(df2, i1=1), days~end, value.var='i1') # days c #1 day1 1
or option using fun.aggregate
dcast(df2, days~end, length) # days c #1 day1 1
as op mentioned dplyr
, involves using first method doesn't have fun.aggregate
library(dplyr) df2 %>% mutate(c=1) %>% select(days:c)
Comments
Post a Comment