2017-01-06 3 views
-1

Ich habe eine Liste in Python in eine Zelle eines csv eingebettet erstellt. Ich versuche, die Elemente in eine Datentabelle in R zu zwingen, aber ich stecke auf einem bestimmten Vektor fest, der Text enthält. Der Grund dafür ist, dass, während strsplit() mit den numerischen Werten durch Aufteilen auf "," gut funktioniert, jedes eingebettete Komma im Text dazu führt, dass ein Vektor länger ist als die anderen. Unten habe ich ein reproduzierbares Beispiel beigefügt. Vielen Dank für Ihre Hilfe!Wie teilt man eine Python-Liste in R

x <- c("['SPOSORSHIP FOR CONVENTION']", "['GENERAL CONTRIBUTION', 'GENERAL CONTRIBUTION']", 
"['WOMEN & POPULATION']", "['PROGRAM SUPPORT', 'PROGRAM SUPPORT']", 
"['MULTIPLE GRANTS FOR MULTIPLE PURPOSES']", "['IMPROVING NATIONAL PARKS']", 
"['general operating support']", "['Civic Engagement', 'Animal Welfare', 'Religion']", 
"['RESEARCH SUBAWARD']", "['OPERATIONAL SUPPORT', 'OPERATIONAL SUPPORT']", 
"['PROMOTE FILM INDUSTRY']", "['TO SUPPORT PUBLIC AFFAIRS PROGRAMS', 'TO SUPPORT PUBLIC AFFAIRS PROGRAMS', 'TO SUPPORT PUBLIC AFFAIRS PROGRAMS', 'TO SUPPORT PUBLIC AFFAIRS PROGRAMS', 'TO SUPPORT PUBLIC AFFAIRS PROGRAMS', 'TO SUPPORT PUBLIC AFFAIRS PROGRAMS']", 
"['10TH ANNUAL GREAT LAKES RESTORATION CONFERENCE AND PETER WEGE TRIBUTE LUNCHEON']", 
"['Conservation', 'Conservation']", "['FOR GENERAL OPERATING SUPPORT']" 
) 
+0

alles in einfachen Anführungszeichen sollte ein eindeutiges Element in einem neuen Zeichenvektor sein. also wäre es analog strsplit und dann unlist ... – StanO

+0

Können Sie die Frage bearbeiten und die Größe Ihres Beispiels auf das Minimum reduzieren, das erforderlich ist, um das Problem neu zu erstellen? –

Antwort

1

Vielleicht wird dies helfen. Ich entfernt zuerst die '[und '] und dann auf split','

cleeaned_text = gsub("(^\\['+)|('\\]\\b)",'',x) #remove '[ and ]' 
unlist(strsplit(cleeaned_text, "', '")) #split on ', ' 
[1] "SPOSORSHIP FOR CONVENTION"              
[2] "GENERAL CONTRIBUTION"               
[3] "GENERAL CONTRIBUTION"               
[4] "WOMEN & POPULATION"                
[5] "PROGRAM SUPPORT"                
[6] "PROGRAM SUPPORT"                
[7] "MULTIPLE GRANTS FOR MULTIPLE PURPOSES"           
[8] "IMPROVING NATIONAL PARKS"              
[9] "general operating support"              
[10] "Civic Engagement"                
[11] "Animal Welfare"                 
[12] "Religion"                  
[13] "RESEARCH SUBAWARD"                
[14] "OPERATIONAL SUPPORT"               
[15] "OPERATIONAL SUPPORT"               
[16] "PROMOTE FILM INDUSTRY"               
[17] "TO SUPPORT PUBLIC AFFAIRS PROGRAMS"            
[18] "TO SUPPORT PUBLIC AFFAIRS PROGRAMS"            
[19] "TO SUPPORT PUBLIC AFFAIRS PROGRAMS"            
[20] "TO SUPPORT PUBLIC AFFAIRS PROGRAMS"            
[21] "TO SUPPORT PUBLIC AFFAIRS PROGRAMS"            
[22] "TO SUPPORT PUBLIC AFFAIRS PROGRAMS"            
[23] "10TH ANNUAL GREAT LAKES RESTORATION CONFERENCE AND PETER WEGE TRIBUTE LUNCHEON" 
[24] "Conservation"                 
[25] "Conservation"                 
[26] "FOR GENERAL OPERATING SUPPORT" 
+1

Das war's! Vielen Dank!!! – StanO

1

Zwei Lösungen:

# with stringr 
library(stringr) 
a <- str_replace_all(x, "\\['|'\\]", "") %>% 
    str_split("', '") %>% 
    unlist 

# with base 
b <- unlist(strsplit(gsub("\\['|'\\]", "", x), "', '")) 

identical(a, b) 

Ergebnis:

[1] "SPOSORSHIP FOR CONVENTION" 
[2] "GENERAL CONTRIBUTION" "GENERAL CONTRIBUTION" 
[3] "WOMEN & POPULATION" 
... 

Der Trick ist, die Saiten zuerst zu trimmen und dann auf ', ' statt nur das Komma trennen.

Verwandte Themen