I think you are looking for pivot_wider()
from tidyr
:
library(dplyr)
library(tidyr)
print(data)
#> Market POSITION VALUATION
#> 1 A UP 151
#> 2 A DOWN 151
#> 3 A RIGHT 5,26
#> 4 A LEFT 96,14
#> 5 B RIGHT 107,07
#> 6 B LEFT 96,14
#> 7 B UP 109,25
#> 8 C UP 96,14
#> 9 C DOWN 109,25
#> 10 C RIGHT 650
#> 11 C LEFT 13912
#> 12 C TOP 13912
#> 13 C MIDDLE 13912
data %>%
pivot_wider(names_from = POSITION,
names_glue = "{.value}_{POSITION}",
values_from = VALUATION)
#> # A tibble: 3 x 7
#> Market VALUATION_UP VALUATION_DOWN VALUATION_RIGHT VALUATION_LEFT
#> <chr> <chr> <chr> <chr> <chr>
#> 1 A 151 151 5,26 96,14
#> 2 B 109,25 <NA> 107,07 96,14
#> 3 C 96,14 109,25 650 13912
#> # ... with 2 more variables: VALUATION_TOP <chr>, VALUATION_MIDDLE <chr>
It glues a name of the new columns with VALUATION
name (from which it uses values inside the table) and POSITION value. See documentation examples, they are pretty good.
Data:
data <- structure(list(Market = c("A", "A", "A", "A", "B", "B", "B",
"C", "C", "C", "C", "C", "C"), POSITION = c("UP", "DOWN", "RIGHT",
"LEFT", "RIGHT", "LEFT", "UP", "UP", "DOWN", "RIGHT", "LEFT",
"TOP", "MIDDLE"), VALUATION = c("151", "151", "5,26", "96,14",
"107,07", "96,14", "109,25", "96,14", "109,25", "650", "13912",
"13912", "13912")), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"
))
Created on 2020-07-03 by the reprex package (v0.3.0)