Reputation: 21
Let's say there is a table for showing "which ID accessed to which organisation at which time". In the table, there is a column for ID, Organisation, and the accessed time. As one can access several organisations in the different time, some IDs can be repeated in the ID column if they accessed other organisations as well.
I want to extract ID and Organisation columns from the initial table, and in the new table, I want to convert the data of organisation into a variable(column) and show how IDs matched to each organisations.
Example data:
ID <- c( 001, 002, 003, 004, 005, 002)
Organisation <- c( "A", "B", "C", "A","D", "A")
Accessed.Time <- c("17:00", "17:00", "17:00", "17:00", "17:00", "17:02")
df1<-data.frame(ID,Organisation,Accessed.Time)
The expected outcome is as follows, For example, (0=no visit, 1= accessed)
ID Organisation.A Organisation.B Organisation.C
001 1 0 1
002 0 0 1
003 1 1 1
I cannot find which package or function I need to use for that case. Especially, is there anyone who know how to extract data in a column and then spread them as an individual column in a new table?
Upvotes: 1
Views: 31
Reputation: 4328
Here's a tidyverse
solution:
library(tidyverse)
df1 %>%
mutate(Accessed.Time = 1) %>%
spread(Organisation, Accessed.Time, fill = 0)
or
df1 %>%
count(ID, Organisation) %>%
spread(Organisation, n, fill = 0)
I think the second solution is a bit more elegant, but the result is the same.
Upvotes: 1