Skip to content

Count data with several numbers separated by commas

3 messages · Xiyan Lon, Simon Blomberg, Bill Venables

#
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
#
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))))
1 2 3 4 5 
9 6 2 2 8

Cheers,

Simon.
On Thu, 2009-04-16 at 13:03 +0700, Xiyan Lon wrote:
#
It rather depends on how you have your data stored.  Here is one possibility you might want to look at:
+ 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")
[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
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.