I want to pivot multiple columns, two by two into an already existing couple, from this
have <- tribble(
~egtest,~egorres, ~egorresu, ~hrorres,~hrorresu,~prorres,~prorresu,~uninteresing,
"qt", 500,"msec",90,"bpm",100,"msec", "cat",
"qtc", 370,"msec",NA,"bpm",103,"msec","dog",
"pra",83,"msec",79,"bpm",97,"msec","cat"
)
To this :
want <- tribble(
~egtest,~egorres, ~egorresu,~uninteresting,
"qt", 500,"msec","cat",
"qtc", 370,"msec","dog",
"pra",83,"msec","cat",
"hr",90,"bpm","cat",
"pr",100,"msec","cat",
"hr",NA,"bpm","dog",
"pr",103,"msec","dog",
"hr",79,"bpm","cat",
"pr",97,"msec","dog"
)
For now my code is
colstopivotEG <- function(table){
out <- subset(colnames(table),grepl(pattern = "orres\\b",colnames(table)))
out <- out[out != "egorres"]
#print(out)
return(out)
}
pivot_eg <- function(ndf){
EG1 <- pivot_longer(ndf,
cols = colstopivotEG(ndf),
names_pattern = "(.*)orres",
names_to="egtest",
values_to="egorres")
EG2 <- pivot_longer(ndf,
cols=ends_with("orresu"),
names_pattern = "(.*)orresu",
values_to="egorresu")
ndf <- bind_cols(EG1,EG2 %>% select(EGORRESU_STD))
}
But I can't seem to be able to pivot into an existing column, I'm out of ideas and any help could be great thanks !
PS: There's a lot of column that don't want to be pivoted