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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…