I want to convert data frame like this:
mre <- tibble::tribble(
~folder3, ~folder2, ~folder1,
"V3=4", "V2=1", "V1=0",
"V3=5", "V2=1", "V1=0",
"V3=4", "V2=2", "V1=0",
"V3=5", "V2=2", "V1=0",
"V3=4", "V2=1", "V1=1",
"V3=5", "V2=1", "V1=1",
"V3=4", "V2=2", "V1=1",
"V3=5", "V2=2", "V1=1"
)
to this:
folder3 folder2 folder1 V3 V2 V1
V3=4 V2=1 V1=0 4 1 0
V3=5 V2=1 V1=0 5 1 0
V3=4 V2=2 V1=0 4 2 0
V3=5 V2=2 V1=0 5 2 0
V3=4 V2=1 V1=1 4 1 1
V3=5 V2=1 V1=1 5 1 1
V3=4 V2=2 V1=1 4 2 1
V3=5 V2=2 V1=1 5 2 1
Basically extracting the unique variable names ("V3, "V2", "V1" here, but could be any valid names such as "a", "b", c" ) for each folder?
column as the new column name, and keep the values in place.
I have the following for a single "folder" column by using the first row value:
mre %>%
tidyr::extract(folder1, into = .$folder1[1] |> word(1, sep="="), "\\S+=(\\d+)", remove = FALSE)
But I don't know how to expand to multiple "folders" columns (the number is not fixed). I tried to use map
following the answers here, but could not figure out how to get the variable names from the first row.
Any suggestions?