I'm new to R, hence this elementary question.
I have a data frame with ~700 rows and 25 columns. Each row is a single appointment with the information about that appointment (time, priority, gender). The rows have a unique identifier in the form of a 7 digit number and there are multiple rows for the same identifier (when the same person came in for more than one appointment).
ID | PRIORITY | TIME |
---|---|---|
234 | Reading | 10/29 |
546 | Writing | 10/30 |
678 | Communication | 10/29 |
546 | Communication | 11/1 |
234 | Writing | 11/1 |
What I would like to do is create a new dataframe that has each unique ID along with the priority of their first visit, second visit, etc.
ID | PRIORITY 1 | PRIORITY 2 |
---|---|---|
234 | Reading | Writing |
546 | Writing | Communication |
678 | Communication |
So far I have the list of all unique identifiers:
uniqueID <- unique(data$ID)
Now I would like to pull the data from PRIORITY based on these unique identifiers.
CodePudding user response:
You can do:
df <- data.frame(ID = c(234, 546, 678, 546, 234),
PRIORITY = c("Reading", "Writing", "Communication", "Communication", "Writing"),
TIME = c("10/29", "10/30", "10/29", "11/1", "11/1"))
library(tidyverse)
df %>%
group_by(ID) %>%
mutate(ID_count = 1:n()) %>%
ungroup() %>%
pivot_wider(id_cols = ID,
values_from = c(PRIORITY, TIME),
names_from = ID_count)
which gives:
# A tibble: 3 x 5
ID PRIORITY_1 PRIORITY_2 TIME_1 TIME_2
<dbl> <chr> <chr> <chr> <chr>
1 234 Reading Writing 10/29 11/1
2 546 Writing Communication 10/30 11/1
3 678 Communication <NA> 10/29 <NA>
CodePudding user response:
This would be another option also:
library(dplyr)
library(tidyr)
dummy_data <- data.frame(
"ID" = c(234,546,678,546,234),
"PRIORITY" = c("Reading","Writing","Communication","Communication","Writing"),
"TIME" = c("10/29","10/30","10/29","11/1","11/1"))
income_data_drop <- dummy_data %>% pivot_wider(names_from = "TIME", values_from = "PRIORITY")
income_data_drop
ID `10/29` `10/30` `11/1`
<dbl> <chr> <chr> <chr>
1 234 Reading <NA> Writing
2 546 <NA> Writing Communication
3 678 Communication <NA> <NA>