Posts

Showing posts with the label dplyr

Converting between long and short data.frame formats

Converting between long and short data.frame formats Simple (?) tidyr question: tidyr I have a data.frame which has several grouping columns and several value columns, in long format. I want to convert it to short (wide) format, where the key would be one of the grouping columns and the resulting data.frame would have a column for each combination of all the other grouping columns and each of the value columns. data.frame key data.frame Here's my long-format data.frame : data.frame set.seed(1) library(dplyr) df <- data.frame(treatment = rep(c(rep("T1",3),rep("T2",3)),2), species = c(rep("S1",6),rep("S2",6)), group = rep(LETTERS[1:3],4), n = as.integer(runif(12,10,20))) %>% dplyr::group_by(treatment,species) %>% dplyr::mutate(freq = n/sum(n)) And here's what I want the resulting wide-format data.frame to be: data.frame res.df <- data.frame(group = LETTERS[1:3], ...

Shiny app: No error reported, but why the VAR6A_2 is empty?

Shiny app: No error reported, but why the VAR6A_2 is empty? Hey I have a question with shiny. After I click on 'Run App", no error is reported, but the tabpanel VAR 6A_2 is empty.. I tried the code outside the app, the code works. But after I move the code inside the app, no result is showed. So confusing. Another question is, I need to create more than 30 tabpanels for the app. Each tabpanle has one variable. What makes thing complicated is, for some of the variables, they are computed with their above variables. For example, Var 4 is computed from Var 2 and Var 3. Here I store all the variables in one app. It is a little confusing because there are so many variables. Is there a better way to organize them? Thanks a lot. Here is the code, thanks. library(shiny) library(plotly) #library(d3heatmap) library(dplyr) #library(ggplot2) #library(ggthemes) #library(gplots) library(readxl) library(tidyr) #source('./utils.R') Index_2 <- read_xlsx('Index 2.xlsx') Index...

select only rows with duplicate id and specific value from another column in R

select only rows with duplicate id and specific value from another column in R I have the following data with ID and value: id <- c("1103-5","1103-5","1104-2","1104-2","1104-4","1104-4","1106-2","1106-2","1106-3","1106-3","2294-1","2294-1","2294-2","2294-2","2294-2","2294-3","2294-3","2294-3","2294-4","2294-4","2294-5","2294-5","2294-5","2300-1","2300-1","2300-2","2300-2","2300-4","2300-4","2321-1","2321-1","2321-2","2321-2","2321-3","2321-3","2321-4","2321-4","2347-1","2347-1","2347-2","2347-2") value <- c(6,3,6,3,6,3,6,3,6,3,3,6,9,3,6,9,3,6,3,6,9,3,6,9,6,9,6,9,6,9,3,9,3,9,3,9...