[R] Count data with several numbers separated by commas

Simon Blomberg s.blomberg1 at uq.edu.au
Thu Apr 16 08:43:57 CEST 2009


Here's a solution, though it may be overcomplicated. I assume the data
frame is called "dat":


 vec <- unlist(lapply(strsplit(dat$x1, ","), function (x)
summary(as.factor(x))))

> table(names(vec))

1 2 3 4 5 
9 6 2 2 8

Cheers,

Simon.

On Thu, 2009-04-16 at 13:03 +0700, Xiyan Lon wrote:
> Dear all,
> I have a data file with 3 variables (x1, x2, x3) where variable x1
> have data that consists of several numbers separated by commas.
> 
> id name	 x1	  x2	x3
> aa101	 1,4,5	  2	1
> aa102	 1,2,5	  1	2
> aa103	 1,2,5	  1	1
> aa104	 1,2,3	  1	2
> aa105	 1,5	  2	2
> aa106	 1,2,5	  2	2
> aa107	 1,2,5	  2	1
> aa108	 1,4,5	  2	1
> aa109	 1,2	  1	2
> aa110	 3,5	  1	2
> 			
> 
> I want to count the number of data for each variables and make barplot
> for each variables.
> I know how to count for variable x2 and x3 and make barplot for x2 and
> x3, but I don't know how to count data in variable x1.
> Are there any trick how to count data in variable x1?
> The result maybe like:
> 
> x1
> 1 9
> 2 6
> 3 2
> 4 4
> 5 8
> 
> 
> x2
> 1 5
> 2 5
> 
> x3
> 1 4
> 2 6
> 
> 
> Thank you for any help.
> 
> Xiyanlon
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
-- 
Simon Blomberg, BSc (Hons), PhD, MAppStat. 
Lecturer and Consultant Statistician 
School of Biological Sciences
The University of Queensland 
St. Lucia Queensland 4072 
Australia
Room 320 Goddard Building (8)
T: +61 7 3365 2506
http://www.uq.edu.au/~uqsblomb
email: S.Blomberg1_at_uq.edu.au

Policies:
1.  I will NOT analyse your data for you.
2.  Your deadline is your problem.

The combination of some data and an aching desire for 
an answer does not ensure that a reasonable answer can 
be extracted from a given body of data. - John Tukey.




More information about the R-help mailing list