Expand grid with cols and rows numbers instead of col names

Hi,
I will try to explain as clearly as possible:
I have got a dataframe with a following col names:
"P18" "P19" "P20" "P21" "P22" "P23" "P35" "P36" "P37" "P38" "P39" "P40" "P41".

With that code:

res <- expand_grid(set1 = paste0("P", 18:23),
                   set2 = paste0("P", 35:41))

it gives me this:

obraz

These values are at the same time col names in my dataframe:

I would like to test correlation between variables P18:P23 and P35:P41.

I would like to make a dataframe probably maybe with expand.grid similar to df on my first picture but with two additional columns indicating col numbers in order to see which col is paired with which regarding my original dataframe ?

obraz

Any ideas would be greatly appreciated, thank you.

dfM <- structure(list(P18 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 0, 1, 1), P19 = c(1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1,
1, 0, 0, 1, 0, 0, 1, 0, 1), P20 = c(1, 0, 0, 0, 1, 1, 1, 0, 0,
0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1), P21 = c(1, 0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1), P22 = c(1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1), P23 = c(0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0), P35 = c(1,
1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1), P36 = c(0,
0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0), P37 = c(0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0), P38 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), P39 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0), P40 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), P41 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), class = c("data.table",
"data.frame"), row.names = c(NA, -20L))

I try with this code but is not giving me what I want, please help:

which(colnames(dfM) =='[P\\d+]' , arr.ind=TRUE)
library(tidyverse)
mylevels <- c(paste0("P", 18:23),
              paste0("P", 35:41))

res <- expand_grid(set1 = paste0("P", 18:23),
                   set2 = paste0("P", 35:41)) |> mutate(c1=as.integer(factor(set1,levels=mylevels)),
                                                        c2=as.integer(factor(set2,levels=mylevels)))

Thank you,

Could you please explain why my attempts failed with the following code:

match("^[P\\d+]", colnames(dfM))

which(names(dfM)=="^P\\d+")

which(colnames(dfM)=="^P\\d+")

which(colnames(dfM) %in% res)

all of the above gave me integer(0) or NA as a result.

regex will only 'work' when functions that use regex are used. == is no such a function. That covers the 3 statements you used that start which( ; also I dont believe match uses regex, just literals.

here are some examples that do use regex

grep(pattern = '[P(\\d+)]',x=colnames(dfM) )
which(str_detect(string=colnames(dfM), pattern = '[P(\\d+)]'))

Thank you, much appreciated.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.