[R] Count data with several numbers separated by commas

Bill.Venables at csiro.au Bill.Venables at csiro.au
Thu Apr 16 09:25:29 CEST 2009


It rather depends on how you have your data stored.  Here is one possibility you might want to look at:

> con <- textConnection("
+ 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")
> 
> dat <- read.table(con, header = TRUE)
> 
> x1_all <- as.numeric(unlist(strsplit(as.character(dat$x1), ",")))
> 
> x1_all
 [1] 1 4 5 1 2 5 1 2 5 1 2 3 1 5 1 2 5 1 2 5 1 4 5 1 2 3 5
> table(x1_all)
x1_all
1 2 3 4 5 
9 6 2 2 8 
> 

Bill Venables
http://www.cmis.csiro.au/bill.venables/ 


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Xiyan Lon
Sent: Thursday, 16 April 2009 4:04 PM
To: r-help at stat.math.ethz.ch
Subject: [R] Count data with several numbers separated by commas

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.




More information about the R-help mailing list