[R] split string and count

arun smartpink111 at yahoo.com
Sun Jun 1 03:37:45 CEST 2014


Hi,

May be this helps:
vec1 <- c("Humanities, Multidisciplinary; Social Sciences, Interdisciplinary; History",
"History","Sociology; Religion","Humanities, Multidisciplinary;","Social Sciences, Interdisciplinary","Literature","Sociology; Religion") 

res1 <- as.data.frame(table(unlist(strsplit(vec1,split="; ?"))))
#or
 res2 <- as.data.frame(table(strsplit(gsub(";+",";",paste(vec1,collapse="; ")),"; ?")[[1]]))
 identical(res1, res2)
#[1] TRUE

Regarding sorting, check ?sort() or ?order.

res2[order(res2$Var1),]


A.K.




Hi, I'm new user to R.
I've a list of terms in vector x, for example:
1 Humanities, Multidisciplinary; Social Sciences, Interdisciplinary; History
2 History
3 Sociology; Religion
4 Humanities, Multidisciplinary; Social Sciences, Interdisciplinary
5 Literature
6 Sociology; Religion

I need split for ';' the variable x, sort by name or by count, into new variable and count. For example:
Humanities, Multidisciplinary 2
History 2
Sociology 2
Religion 2
Literature 1
Social Sciences, Interdisciplinary 2

I don't know how many time ";" appears

Thanks. 




More information about the R-help mailing list