[R] Question about expand.grid function in R
Hi, I am extremely new to R, and was wondering if someone would be able to help me with a question regarding the expand.grid function. When I input expand.grid.rep - function(x, n=1) do.call(expand.grid, rep(list(x),n)) expand.grid.rep(c(a, b, c), 3) my output is as follows, Var1 Var2 Var3 1 a a a 2 b a a 3 c a a 4 a b a 5 b b a 6 c b a 7 a c a 8 b c a 9 c c a 10 a a b 11 b a b 12 c a b 13 a b b 14 b b b 15 c b b 16 a c b 17 b c b 18 c c b 19 a a c 20 b a c 21 c a c 22 a b c 23 b b c 24 c b c 25 a c c 26 b c c 27 c c c I was wondering if there was anyway I can change the row numbers to labels that indicate what is in each row. Instead of a 1, I'd like to have a label saying aaa, etc. I'm not sure if this is even possible within the expand.grid function but any help would be appreciated. Thanks. [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Question about expand.grid function in R
I don't think you can do it within expand.grid() but something like this might work: rownames(x) - apply(x, 1, paste, collapse = ) Michael On Tue, May 1, 2012 at 5:05 AM, Kelly Cool kellycoo...@yahoo.com wrote: Hi, I am extremely new to R, and was wondering if someone would be able to help me with a question regarding the expand.grid function. When I input expand.grid.rep - function(x, n=1) do.call(expand.grid, rep(list(x),n)) expand.grid.rep(c(a, b, c), 3) my output is as follows, Var1 Var2 Var3 1 a a a 2 b a a 3 c a a 4 a b a 5 b b a 6 c b a 7 a c a 8 b c a 9 c c a 10 a a b 11 b a b 12 c a b 13 a b b 14 b b b 15 c b b 16 a c b 17 b c b 18 c c b 19 a a c 20 b a c 21 c a c 22 a b c 23 b b c 24 c b c 25 a c c 26 b c c 27 c c c I was wondering if there was anyway I can change the row numbers to labels that indicate what is in each row. Instead of a 1, I'd like to have a label saying aaa, etc. I'm not sure if this is even possible within the expand.grid function but any help would be appreciated. Thanks. [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.
Re: [R] Question about expand.grid function in R
On May 1, 2012, at 15:36 , R. Michael Weylandt wrote: I don't think you can do it within expand.grid() but something like this might work: rownames(x) - apply(x, 1, paste, collapse = ) Also rownames(x) - do.call(paste, c(x, sep=)) or, in recent versions, rownames(x) - do.call(paste0, x) Michael On Tue, May 1, 2012 at 5:05 AM, Kelly Cool kellycoo...@yahoo.com wrote: Hi, I am extremely new to R, and was wondering if someone would be able to help me with a question regarding the expand.grid function. When I input expand.grid.rep - function(x, n=1) do.call(expand.grid, rep(list(x),n)) expand.grid.rep(c(a, b, c), 3) my output is as follows, Var1 Var2 Var3 1 aaa 2 baa 3 caa 4 aba 5 bba 6 cba 7 aca 8 bca 9 cca 10aab 11bab 12cab 13abb 14bbb 15cbb 16acb 17bcb 18ccb 19aac 20bac 21cac 22abc 23bbc 24cbc 25acc 26bcc 27ccc I was wondering if there was anyway I can change the row numbers to labels that indicate what is in each row. Instead of a 1, I'd like to have a label saying aaa, etc. I'm not sure if this is even possible within the expand.grid function but any help would be appreciated. Thanks. [[alternative HTML version deleted]] __ R-help@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. __ R-help@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. -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd@cbs.dk Priv: pda...@gmail.com __ R-help@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.