r - Create TRUE/FALSE dataframe based on the presence/absence of specific variables -
i have data frame samples taken different seasons. summarize sites have samples spring (march-may) , autumn (september-november) across different years. example, if site had sample spring 2007, cell reads 'true'. here example dataset:
dates <- data.frame(c(as.date("2007-9-1"), rep(as.date("2008-3-1"), times = 3) , rep(as.date("2008-9-1"), times = 3))) sites <- as.data.frame(as.factor(c("sitea",rep(c("sitea","siteb","sitec"), 2)))) values <- data.frame(matrix(sample(0:50, 3.5*2, replace=true), ncol=1)) dataframe <- cbind(dates,sites,values) colnames(dataframe) <- c("date","site","value")
i have managed create factor 'season' within dataframe based on these functions.
dataframe$months <- as.numeric(format(dataframe$date, '%m')) dataframe$season <- cut(dataframe$months, breaks = c(1, 2, 5, 8, 11, 12), labels = c("winter", "spring", "summer", "autumn", "winter"), right = false)
but unsure go here. here output should like.
a <- rep("true",times = 3) b <- c("false",rep("true",times = 2)) c <- c("false",rep("true",times = 2)) output <- as.data.frame(rbind(a,b,c)) colnames(output) <- c("autumn.07","spring.07","autumn.08")
here proposition:
dataframe$samplings <- interaction(dataframe$season, unlist(lapply(strsplit(as.character(dataframe$date), '-'), function(x) x[[1]]) )) u1 <- unique(dataframe$site) u2 <- unique(dataframe$samplings) output <- matrix( matrix(levels(interaction(u1, u2)), nrow=length(unique(dataframe$site))) %in% interaction(dataframe$site,dataframe$samplings), nrow=length(unique(dataframe$site)) ) colnames(output) <- levels(dataframe$samplings) rownames(output) <- unique(dataframe$site) output # time interactions # can clear output[, apply(output, 2, sum) != 0]
Comments
Post a Comment