Re: [R] Simple loop question

2014-03-29 Thread Noah Marconi
Here're a couple alternatives if you want to use the index instead of the variable name: # Reproducible data frame a1 - 1:15 a2 - letters[1:15] a3 - LETTERS[1:15] a4 - 15:1 a5 - letters[15:1] df - data.frame(a1, a2, a3, a4, a5) df # a1 a2 a3 a4 a5 # 1 1 a A 15 o # 2 2 b B 14 n # 3

Re: [R] numeric to factor via lookup table

2014-03-29 Thread Noah Marconi
Is this what you're going for? factor(values, levels=mylevels[[1]], labels=mylevels[[2]]) [1] a b e e j Levels: a b c d e f g h i j On 2014-03-28 16:38, Jonathan Greenberg wrote: R-helpers: Hopefully this is an easy one. Given a lookup table: mylevels -

Re: [R] Consistent size dashes...

2014-03-29 Thread Noah Marconi
If you rename two or more levels with the same string, R collapses them. Using gsub like you suggest works well. If there are more than the two characters causing problems you may need to expand the pattern that's being matched. With the example you sent, this works: df - structure(c(6L, 4L,

Re: [R] How to rbind() two matrices that have different column sizes?

2014-03-26 Thread Noah Marconi
Out of curiosity, what's the reason for not using plyr? If it has to do with installing the package, here's how the rbind.fill function is implemented in plyr. It may help show you what you'd like to do: plyr::rbind.fill function (...) { dfs - list(...) if (length(dfs) == 0)

Re: [R] dataframe calculations based on certain values of a column

2014-03-26 Thread Noah Marconi
dplyr's group_by and mutate can create those columns for you: var1 - c(a,b,c,a,b,c,a,b,c) var2 - c(X,X,X,Y,Y,Y,Z,Z,Z) var3 - c(1,2,2,5,2,6,7,4,4) df - data.frame(var1,var2,var3) dt - tbl_df(df) dt %.% group_by(var2) %.% mutate( div = var3[var1 == c], result_calc = var3/div )

Re: [R] Analyse the effect of group membership

2014-03-24 Thread Noah Marconi
Does this help? set.seed(500) mtx - matrix(round(runif(100,0,1)), ncol=5, dimnames=list(Subject=1:20, Group=c('A','B','C','D','E'))) groupCombo - apply(mtx, 1, function(x){ x - paste( names(x)[as.logical(x)], collapse='+' ); return(x) })

Re: [R] function Stack

2014-03-24 Thread Noah Marconi
The reshape2 package may be what you're looking for: http://cran.r-project.org/web/packages/reshape2/index.html install.packages(reshape2) reshape2::melt(df) On 2014-03-24 12:02, Tham Tran wrote: Dears R Users, I have another question on function stack. Given a data frame like:

Re: [R] function Stack

2014-03-24 Thread Noah Marconi
I sent that last email too quickly, the duplicate column names cause a problem when melting. Transposing the data frame first gets around it: reshape2::melt(t(df))[c('Var1', 'value')] Var1 value 1a 3 2b 2 3a 9 4b 6 5a 5 6b 8 7a 1 8b

Re: [R] Row names at the end of the record

2014-03-19 Thread Noah Marconi
Is this what you're trying to do? mtrx - structure(c(BB, AB, BB, BB, BB, BB, AA, BB, BB, BB, BB, BB, AB, AB, BB, AA, BB, BB, BB, BB, BB, AB, BB, BB, BB), .Dim = c(5L, 5L), .Dimnames = list( c(rs1, rs2, rs3, rs4, rs5), c(LG_1,