Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
757 views
in Technique[技术] by (71.8m points)

r - Split strings in a matrix column and count the single elements in a new vector

I have a dataset like this from a file read into R with read.table():

Nr Result1
 1 "A203,A305,A409,B309,B424,B545"
 2 "A190,A203,A305,B309,B425,B545"
 3 "A203,A305,A410,B280,B309,B425,B545"

Result1 is a string that I would like to split at the "," to count the occurrence of each element in each row. I would like to count the different elements and write out the result in this format:

A190 A203 A305 A409 A410 B280 B309 B424 B425 B545
1    3    3    1    1    1    3    1    2    3

My first thought is to loop over every row, split the string into single elements, create a vector with the first set of elements and for the second row, check if matching element already exists (count+1) or append unknown element to vector with count=1.

I am quite new to R and would appreciate some example code or hints how to implement the single steps with R functions! Many Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think this is what you are looking for:

newvector <- table(unlist(strsplit(as.character(df$Result1), ",")))

Result (stored in newvector):

#>newvector
#A190 A203 A305 A409 A410 B280 B309 B424 B425 B545 
#   1    3    3    1    1    1    3    1    2    3 

The strsplit function splits the vector of strings (Result1) at each comma. The result is a list for each row of your data.frame (df in my example). To turn this list into a vector you use unlist. The table function then creates a table with the frequencies.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...