How can I do pivot_wider but for characters?

Hello! I would like to split a column from a data frame into 2 columns based on the character of each row of that column.
E.g.
Monday 3
Tuesday 5
Tuesday 5
Monday 6

I would like to have 2 columns like this:
Monday Tuesday
3 5
6 5

Thank you in advance

P.S. the 5's under the tuesday column,

There are a couple of things that are not clear about your data.

  1. Does the original data have one or two columns?
  2. How can you tell the grouping of the days? Is there another column that marks which Monday goes with which Tuesday or do you simply want Mondays and Tuesdays listed in the order in which they appear?

The original data has 1 column for the days and 1 column for the corresponding values.

Then I would like to make a column with the name Monday showing the values of Mondays and column named Tuesday showing only the values of Tuesdays

Like this

I hope now it is clearer, Iam really sorry but Iam quite new in R-studio. Thank you once more.

The key to doing this is that there needs to be a third column that tells R how to group the days and values. In the image you posted, this is information is in the country column. For example, there is only one value of cases for the year 1999 for Angola. The original data you posted has no column that groups the days. If I add one named ID, the pivoting is simple.

library(tidyr)
DF <- data.frame(ID = c(1,1,2,2), 
                 Day = c("Monday", "Tuesday", "Tuesday", "Monday"),
                 Value = c(3,5,5,6))
DF
#>   ID     Day Value
#> 1  1  Monday     3
#> 2  1 Tuesday     5
#> 3  2 Tuesday     5
#> 4  2  Monday     6
DFwide <- DF |> pivot_wider(names_from = "Day", values_from = "Value")
DFwide
#> # A tibble: 2 × 3
#>      ID Monday Tuesday
#>   <dbl>  <dbl>   <dbl>
#> 1     1      3       5
#> 2     2      6       5

Created on 2022-12-05 with reprex v2.0.2
If your real data do not have another column, one can be made but the details of how to do it depend on how the data are organized. What is the rule for grouping a Monday with its corresponding Tuesday?

Thank you so much for your help. I just wanted to have them by days :slight_smile:

This topic was automatically closed 21 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.