I am trying to transform a dataset: : https://i.stack.imgur.com/09Ioo.png To something like this: : https://i.stack.imgur.com/vKKu2.png How can I do this on R? I tried using gather() but somehow im not getting the results.. library(tidyverse) df_gather <- df %>% gather(key = "Day", "Sensor",2:5) View(df_gather) Thanks in advance for your help! >Solution : Because you did not… Read More tidyverse: data transformation, gather()
assume this is my dataframe ( already created by a sequence of pipes ) df <- data.frame(month = c("May", "May","May","June","June","June", "July", "July","July"), rate = rep(c(0.3, 0.4, 0.5),times=3), a = c(5,4,3,10,9,8,20,19,18), b = c(100,200,300,1,2,3,10,20,30)) df month rate a b 1 May 0.3 5 100 2 May 0.4 4 200 3 May 0.5 3 300 4 June… Read More create two dataframes at once and joint their columns alternatively in R
I have a df with this structure id 1 2 3 5 1 1 0 2 0 2 3 4 1 0 3 1 1 0 2 What I would like is to complete it as I was need it to conform to a format that goes on columns from 1 thru 6, so the… Read More Create columns if missing in dataframe with R (tidyverse)
I want to extract the random effects from my lmer model, including the person this random effect belongs to. My goal is to create a tibble that has one column for the person and another column for the random effect. Using coef(modelA)$bib I am able to extract the random effect to a list. Here I… Read More Extracting index value from a list in R?
I want to create a variable which identifies the first occurance of a variable in a column but I cannot seem to get the code to work. The new varibale should only mark a nonNA index which is the first occurance of this variable and ideally function within a piped code chunk. I have tried… Read More Mutate variable conditional on first unique occurance of another variable
Suppose we have a data frame df like book_id book_category book_word_hi book_word_bye book_word_yes 1 drama 3 0 4 2 action 1 4 5 3 drama 5 3 2 I would like to count the number of values within the book_word columns and sum them in a table for each book_category. So the output here should… Read More How to count number of values in columns based on a category in R?
I’ve got a big data frame, and like to remove the duplicate column For simplicity, let’s pretend this is my data: df <- data.frame(id1 = c("Aa","Aa","Ba","Ca","Da"), id2 = c(2,1,4,5,10), location=c(351,261,101,91,51), comment=c(35,26,10,9,5), comment=c(5,16,25,14,11), hight=c(15,21,5,19,18), check.names = FALSE) I can remove the duplicate column name "comment" using: df <- df[!duplicated(colnames(df))] However, when I apply same code in… Read More How to remove duplicate column names in R?
I would like to check whether in a text there are a) three consonants in a row or b) four identical letters in a row. Can someone please help me with the regular expressions? library(tidyverse) df <- data.frame(text = c("Completely valid", "abcdefg", "blablabla", "flahaaaa", "asdf", "another text", "a last one", "sj", "ngbas")) consonants <- c("q",… Read More Problem with regex (check string for certain repetitions)
I want to group by ID and summarise, whilst removing NAs. please see example code below. # Example data ID <- c(1, 1, 1, 2, 2, 3, 3) x <- c(2, 3, NA, 2, 3, 1, 1) ID_x <- tibble(ID, x) # 1. Works ID_x %>% group_by(ID) %>% summarise_each(mean) # 2. Does not work with… Read More How can I in R, group by ID and summarise by mean with na.rm = TRUE
I’m trying to apply some equations to get the proportion of a line (numeric array) that coincides with another line (another numeric array). I have a data frame with the required values and I try to create a new column with the percentage outcome based on how the two lines coincide. I have checked the… Read More case_when fails (unexpected results) but conditions for case_when work outside