Sebastian Zeki
Sebastian Zeki

Reputation: 6874

How to split a list with named characters

I have a list as below. I would like to split the elements of the list into a dataframe so that I capture the first, second and last "" in the list. I have spent hours googling but not sure where to start. I tried the split function as roman<-split(roman,f=roman$V1) but I got an error and anyway I don't know how to select out the first two and last elements of the vector.

I think things are complicated by the structure of the list. For example one of the columns is

> head(dput(roman[,1:6]))
list(structure(c("Gagner", "M", "M", "Department of Surgery, Herbert Wertheim School of Medicine, Florida International University, Miami, FL, USA. [email protected].", 
"Department of Surgery, Hôpital du Sacre Coeur, Montreal, Canada. [email protected]."
), .Names = c("LastName", "ForeName", "Initials", "AffiliationInfo", 
"AffiliationInfo")), structure(c("Braghetto", "I", "I", "Department of Surgery, University Hospital \"Dr. Jose J. Aguirre\". Faculty of Medicine, University of Chile, Santiago, Chile."
), .Names = c("LastName", "ForeName", "Initials", "AffiliationInfo"
)), structure(c("Iwaya", "Yugo", "Y", "Department of Gastroenterology, Nagano Municipal Hospital, Nagano, Japan.", 
"Department of Medicine, Division of Gastroenterology, Shinshu University School of Medicine, Matsumoto, Japan."
), .Names = c("LastName", "ForeName", "Initials", "AffiliationInfo", 
"AffiliationInfo")))
[[1]]
                                                                                                                                LastName 
                                                                                                                                "Gagner" 
                                                                                                                                ForeName 
                                                                                                                                     "M" 
                                                                                                                                Initials 
                                                                                                                                     "M" 
                                                                                                                         AffiliationInfo 
"Department of Surgery, Herbert Wertheim School of Medicine, Florida International University, Miami, FL, USA. [email protected]." 
                                                                                                                         AffiliationInfo 
                                             "Department of Surgery, Hôpital du Sacre Coeur, Montreal, Canada. [email protected]." 

[[2]]
                                                                                                                        LastName 
                                                                                                                     "Braghetto" 
                                                                                                                        ForeName 
                                                                                                                             "I" 
                                                                                                                        Initials 
                                                                                                                             "I" 
                                                                                                                 AffiliationInfo 
"Department of Surgery, University Hospital \"Dr. Jose J. Aguirre\". Faculty of Medicine, University of Chile, Santiago, Chile." 

[[3]]
                                                                                                        LastName                                                                                                    "Iwaya"                                                                                                         ForeName                                                                                                     "Yugo" 
                                                                                                        Initials 
                                                                                                             "Y" 
                                                                                                 AffiliationInfo 
                                     "Department of Gastroenterology, Nagano Municipal Hospital, Nagano, Japan." 
                                                                                                 AffiliationInfo 
"Department of Medicine, Division of Gastroenterology, Shinshu University School of Medicine, Matsumoto, Japan."

The expected output should be

LastName     FirstName     Affiliation
Gagner            M         Department of Surgery, Hôpital du Sacre Coeur, Montreal, Canada. [email protected].
Braghetto         I         Department of Surgery, University Hospital \"Dr. Jose J. Aguirre\". Faculty of Medicine, University of Chile, Santiago, Chile
Iwaya            Yugo        Department of Medicine, Division of Gastroenterology, Shinshu University School of Medicine, Matsumoto, Japan

Upvotes: 2

Views: 57

Answers (1)

Raad
Raad

Reputation: 2715

This approach using sapply may work for you:

t(sapply(data, function(z) z[c(1, length(z) - 1, length(z))]))

Upvotes: 1

Related Questions