Reputation: 35
I need to split a row of my data into columns. I am new to R it looks like its coming through as one big string but actually I want it as column headers.
Upvotes: 0
Views: 68
Reputation: 11546
Does this work:
library(dplyr)
library(tidyr)
df
# A tibble: 6 x 1
x1
<chr>
1 MeetingID, Date, JetBet, Track
2 49588,2020-11-03,6,Wingatui
3 49588,2020-11-03,6,Wingatui
4 49588,2020-11-03,6,Wingatui
5 49588,2020-11-03,6,Wingatui
6 49588,2020-11-03,6,Wingatui
cnames <- df[1,,drop = T]
df <- df[2:nrow(df), ]
df %>% separate(col = x1, into = trimws(strsplit(cnames,split = ',')[[1]]), sep = ',')
# A tibble: 5 x 4
MeetingID Date JetBet Track
<chr> <chr> <chr> <chr>
1 49588 2020-11-03 6 Wingatui
2 49588 2020-11-03 6 Wingatui
3 49588 2020-11-03 6 Wingatui
4 49588 2020-11-03 6 Wingatui
5 49588 2020-11-03 6 Wingatui
Upvotes: 0