I am working with data that contain 10 columns and 2 rows. Below you can see my data.
df<-data.frame(
'Company with premium status-Land' = c(5,1),
'Company with premium status-Sea' = c(5,1),
'Company with premium status-Air' = c(5,1),
'Company with premium status-Unkown' = c(5,1),
'Company with premium status-Total' = c(5,1),
'Company with low class status-Land' = c(10,1),
'Company with low class status-Sea' = c(10,1),
'Company with low class status-Air' = c(10,1),
'Company with low class status-Unkown' = c(10,1),
'Company with low class status-Total' = c(10,1),
check.names = FALSE)
df
Now I want to pivot this data and split columns by this delimiter -. I tried with this line of codes but it is not working.
df1<-df %>% pivot_longer(c(
"Company with premium status-Land",
"Company with premium status-Sea",
"Company with premium status-Air",
"Company with premium status-Unkown",
"Company with premium status-Total",
"Company with low class status-Land",
"Company with low class status-Sea",
"Company with low class status-Air",
"Company with low class status-Unkown",
"Company with low class status-Total"
), names_to = c("Company with", ".value"),
names_pattern = "(.*)-(.*)")
In the end, I need to have output as the picture shown below
So can anybody help me how to solve this problem?
