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
Simon Blomberg
2009-Apr-16 06:43 UTC
[R] Count data with several numbers separated by commas
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.
Bill.Venables at csiro.au
2009-Apr-16 07:25 UTC
[R] Count data with several numbers separated by commas
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.