How to extract all the strings before the second colon in r?

Advertisements I have a time data looks like this: 2024-01-26 19:24:40.0 I want to extract everything before the second colon like this: 2024-01-26 19:24 I tried the code as below: a="2024-01-26 19:24:40.0" b=sub("(^([^:]+:){2}).*$", "\\1",a) But the result was like this: 2024-01-26 19:24: So how to write the code in the right way? >Solution : Option… Read More How to extract all the strings before the second colon in r?

Filter out individuals who developed diseases in a certain sequence

Advertisements I have a data frame – each individual has multiple visits. I want to filter out patients who had disease 2 after disease 1. In this case, I would pick out ID 2 & 4. ID <- c(1,1,2,2,2,3,3,3,4,4,4,4,5,5) Visit <- c(1,2,1,2,3,1,2,3,1,2,3,4,1,2) Disease <- c(2,2,1,2,1,1,1,1,1,1,1,2,2,1) df <- data.frame(ID, Visit, Disease) >Solution : A dplyr solution:… Read More Filter out individuals who developed diseases in a certain sequence

Adding a column based on counts of another column value in R

Advertisements I have a data frame as follows: comment_id <- c(1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10) cat <- c("acc_sp", "acc_lex", "org_gen", "acc_gen", "ran_lex", "arg_rel", "len", "org_lay", "org_spe", "org_gen", "coh_link") df <- data.frame(comment_id, cat) You’ll notice that there are two items with comment_id = 2. I need to create a new… Read More Adding a column based on counts of another column value in R

Get the categorical value for each column based on sum

Advertisements Say I have a dataframe with families and I have the read counts for each sample. I want to return another dataframe that just shows me the sample in column 1, and which family is likely the biggest contributor to the total read count in that sample. Sample dataframe: structure(list(Family = c("Asteraceae", "Fabaceae", "Plantaginaceae",… Read More Get the categorical value for each column based on sum