How to add "swab" to text string when "dry0" occurs in R

I have a column with swab IDs. The normal ID nomenclature is "dryswab00001". However there are some samples as "dry00135". How would I str_detect those IDs and insert "swab" before the leading zero/number? Data data <- structure(list(swab_id = c("dryswab00001", "dry00002")), class = "data.frame", row.names = c(NA, -2L)) >Solution : You can use str_detect and str_replace… Read More How to add "swab" to text string when "dry0" occurs in R

Reorder matrix rows and columns based on alphabetical order of colnames and rownames in R

I was wondering if there is a way to reorder the rows and columns of the MATRIX below by their rownames and colnames in alphabetical order? MATRIX <- structure(c(0.00096, 0.00047, -0.00027, -0.00018, 4e-04, 0.00022, 0.00047, 0.00105, -3e-04, -8e-05, 0.00018, 0.00041, -0.00027, -3e-04, 0.00071, 3e-05, -4e-05, -8e-05, -0.00018, -8e-05, 3e-05, 0.00066, -0.00023, -0.00024, 4e-04, 0.00018, -4e-05,… Read More Reorder matrix rows and columns based on alphabetical order of colnames and rownames in R

How to add variables, make the it as equally dataframe

There is dataframe raw_data_2 , I want to add variables e_2022 b_2023,the result as dataframe raw_data_2_fixed .How to add them by easy code ? (In actual, i have to add so many variables, type in one by one will NOT be good choice). library(tidyverse) raw_data_2 <- data.frame(category=c(‘A’,’B’,’C’), a_2022=c(1,2,3), b_2022=c(0.1,0.4,0.3), a_2023=c(1.5,3,4), e_2023=c(0.3,0.7,0.5) ) The wished dataframe… Read More How to add variables, make the it as equally dataframe

Remove duplicates regardless of their order and conditionally to other variables

I’m interested in eliminating duplicates, regardless of their order, and conditionally to one variable. To be more specific, I aim to retain the dyad where the one on the right consistently possesses the lowest longitude. Here an example of the data I have: data<-data.frame(dyad.1=c("A","B","A","C","B","C"),dyad.2=c("B","A","C","A","C","B"), long_dyad.1=c(1.3,2,1.3,0.3,2,1.3), long_dyad.2=c(2,1.3,0.3,1.3,1.3,2)) Here an example of what I would like: data<-data.frame(dyad.1=c("A","C","C"),dyad.2=c("B","A","B"),… Read More Remove duplicates regardless of their order and conditionally to other variables

Count unique observations in a grouped variable

I have a dataframe that has about 300,000 rows so I’m trying summarise some of it. It also has about 48 different cols, but for now I’m only interested in 2 of them. Here’s an example of what the data look like dput(df) structure(list(pnum = c("1951bbn", "1951bbn", "1951bbn", "1951bbn", "1951bbn", "1951bbn", "1951bbn", "1951ggl", "1951ggl", "1951ggl",… Read More Count unique observations in a grouped variable

Using str_detect in R to sort a datatable based on a character variable column

I have a datatable with mainly numerical column variables, as well as one character variable containing ~30 different character variables. I would like to use str_detect in R to read information in the column of data type character, compare it to a vector containing a set of strings, create a new dt column, and assign… Read More Using str_detect in R to sort a datatable based on a character variable column

tidyverse filter behaviour I dont expect (%in% doesnt work with pull() )

When I try to filter a dataframe using the %in% operator using a pull() subfilter, It does not work. However, When I store the pull() subquery in a variable, and then use the %in% operator on the variable, It does work. I use as an example the well known mtcars dataset. library(tidyverse) mydf <- tibble(mtcars)… Read More tidyverse filter behaviour I dont expect (%in% doesnt work with pull() )