I have an R df where one column, Author, looks like this (the names themselves are replaced by 'Last' and 'First' in this post for anonymity):
| id | Author |
|---|---|
| 1 | Last, First & Last, First |
| 2 | Last, First & Last, First & Last, First & Last, First |
| 3 | Last, First & Last, First & Last, First |
I need to add a new column for the first and last name of every author. It should look like this:
| id | First1 | Last1 | First2 | Last2 | First3 | Last3 | First4 | Last4 |
|---|---|---|---|---|---|---|---|---|
| 1 | First | Last | First | Last | ||||
| 2 | First | Last | First | Last | First | Last | First | Last |
| 3 | First | Last | First | Last | First | Last |
Of course, not all readings have the same number of authors, so I'm unable to append a specific number of columns for all rows.
** NOTE: I've done this for columns that only have one author using
data <- data %>%
mutate(FirstName=unlist(lapply(strsplit(Author,", "),function(x) x[2])),
LastName=gsub(",.*","",Author))
How can I do this?