Re: [R] rbind with partially overlapping column names
Hi all, Thanks for your responses. The merge output works for the test case as Bill pointed out, but is contingent on non-overlapping values. Thanks for pointing this out Ian. My actual dataset needs to allow for overlapping values (sorry for the poor test case) so I will cook up something like Ian and Dennis suggested. Thanks again. Jonathan On Mon, May 16, 2011 at 12:28 AM, Ian Gow iand...@gmail.com wrote: That approach relies on df1 and df2 not having overlapping values in b. Slight variation in df2 gives different results: df1 - data.frame(a=c(A,A),b=c(B,B)) df2 - data.frame(b=c(B,B),c=c(c,c)) merge(df1,df2,all=TRUE) b a c 1 B A c 2 B A c 3 B A c 4 B A c On 5/15/11 11:19 PM, William Dunlap wdun...@tibco.com wrote: -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Jonathan Flowers Sent: Sunday, May 15, 2011 5:41 PM To: r-help@r-project.org Subject: [R] rbind with partially overlapping column names Hello, I would like to merge two data frames with partially overlapping column names with an rbind-like operation. For the follow data frames, df1 - data.frame(a=c(A,A),b=c(B,B)) df2 - data.frame(b=c(b,b),c=c(c,c)) I would like the output frame to be (with NAs where the frames don't overlap) a b c A B NA A B NA NA b c NA b c I am familiar with ?merge and ?rbind, but neither seem to offer a means to accomplish this. What is wrong with merge(all=TRUE,...)? merge(df1,df2,all=TRUE) bac 1 BA NA 2 BA NA 3 b NAc 4 b NAc Rearrange the columns if that is necessary merge(df1,df2,all=TRUE)[c(a,b,c)] a bc 1A B NA 2A B NA 3 NA bc 4 NA bc Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com Thanks in advance. Jonathan [[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. [[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] rbind with partially overlapping column names
Hello, I would like to merge two data frames with partially overlapping column names with an rbind-like operation. For the follow data frames, df1 - data.frame(a=c(A,A),b=c(B,B)) df2 - data.frame(b=c(b,b),c=c(c,c)) I would like the output frame to be (with NAs where the frames don't overlap) a b c A B NA A B NA NA b c NA b c I am familiar with ?merge and ?rbind, but neither seem to offer a means to accomplish this. Thanks in advance. Jonathan [[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] glm with within-subject factors
Hello, I am analyzing a dataset where the response is count data. I have one two-level factor that is repeated within-subjects and additional between-subject variables that are either categorical or continuous. I have previously modeled a comparable dataset (without the within-subjects factor) using glm with family=poisson, but see no way to incorporate repeated measures with the glm function. Here is a small dataset that illustrates the data structure, note that each subject is repeated twice, once for each level of factor X1: dframe - data.frame(response=rpois(1:10,2),subjects=sort(rep(c(Tom,Nic,Bil,Jon,Jil),2)),X1=rep(c(A,B),5),X2=rep(sapply(1:5,function(x) rnorm(1)),each=2)) Can someone suggest an approach to test for an effect of X1 and X2 on counts? Best, Jonathan [[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] get list element names within lapply / sapply call
Hi all, I would like to iterate through a list with named elements and access the names within an lapply / sapply call. One way to do this is iterate through the names and index the list with the name. Is there a way to iterate through the list elements themselves and access the element names within in the function? For example, mylist - list(a=c(1,2),b=c(3,4),c=c(5,6)) sapply(mylist,function(x){ #get name of list elements (a, b, c) #then do other stuff }) Thanks for your suggestions. Jonathan [[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] get list element names within lapply / sapply call
Thanks Gene. I'll look into the Browse option. Best, Jonathan On Sat, Jan 15, 2011 at 4:36 PM, Gene Leynes gleyne...@gmail.comgleynes%...@gmail.com wrote: I one tried to write a function to do that, but it wasn't worth it / didn't work I found this to be a better solution: mynames = names(sapply(mylist, names)) for(nm in mynames){ print(mylist[nm]) # or do other stuff } You can use browser to look inside sapply, and the objects available don't seem to have the current index information, but I don't know how to look at the ... sapply(mylist, browser) Called from: lapply(X, FUN, ...) Browse[1] ls(all.names=TRUE) [1] ... FUN X Browse[1] FUN function (text = , condition = NULL, expr = TRUE, skipCalls = 0L) .Primitive(browser) Browse[1] X $a [1] 1 2 $b [1] 3 4 $c [1] 5 6 Browse[1] On Sat, Jan 15, 2011 at 3:06 PM, Jonathan Flowers jonathanmflow...@gmail.com wrote: Hi all, I would like to iterate through a list with named elements and access the names within an lapply / sapply call. One way to do this is iterate through the names and index the list with the name. Is there a way to iterate through the list elements themselves and access the element names within in the function? For example, mylist - list(a=c(1,2),b=c(3,4),c=c(5,6)) sapply(mylist,function(x){ #get name of list elements (a, b, c) #then do other stuff }) Thanks for your suggestions. Jonathan [[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. [[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] subset question
Try subd - d[, gene == c(i1,i2,i3)] On Wed, Dec 29, 2010 at 4:55 PM, ANJAN PURKAYASTHA anjan.purkayas...@gmail.com wrote: Hi, I'm having a problem with a step that should be pretty simple. I have a dataframe, d, with column names : gene s1 s2 s3. The column gene stores an Id; the rest of the columns store intensity data. I would like to extract the rows for gene Ids i1, i2, i3 ( I know a priori that those rows exist). So I do this: subset(d, gene %in% c(i1, i2, i3)). This does not give me the required data. Any ideas where I am going wrong? TIA, Anjan -- === anjan purkayastha, phd. research associate fas center for systems biology, harvard university 52 oxford street cambridge ma 02138 phone-703.740.6939 === [[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. [[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] Perl cut equivalent in R
Does anyone know of a command in R that is equivalent to the =cut function in Perl? Thanks. Jonathan [[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] Perl cut equivalent in R
Thanks Sarah, you are right. In perl, cut serves the function of eliminating a block of code from being executed in a script. When =cut is placed above and below the code that you do not wish to execute then the interpreter will skip over the code. There are lots of ways to solve the problem, but cut is a handy solution. Thanks. On Wed, Dec 1, 2010 at 6:31 PM, Sarah Goslee sarah.gos...@gmail.com wrote: It would help if you told us what you wanted this function to do, and provided an example. Not everyone speaks Perl. Sarah On Wed, Dec 1, 2010 at 6:10 PM, Jonathan Flowers jonathanmflow...@gmail.com wrote: Does anyone know of a command in R that is equivalent to the =cut function in Perl? Thanks. Jonathan -- Sarah Goslee http://www.functionaldiversity.org [[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] d.f. in F test of nested glm models
Dear all, I am fitting a glm to count data using poison errors with the log link. My goal is to test for the significance of model terms by calling the anova function on two nested models following the recommendation in Michael Crawley's guide to Statistical Computing. Without going into too much detail, essentially, I have a small overdispersion problem (errors do not fit the poisson assumption) so I am following Crawley's recommendation and setting family=quasipoisson and using an F test (rather than a chi-square test) to test for significance. This is working fine, but I cannot figure out how the F value in the analysis of deviance table was obtained and what degrees of freedom were used to obtain the P value (essentially I don't know how to report the result). The following example (while errors are not overdispersed) otherwise generates a comparable analysis of deviance table to my analysis. Any help would be much appreciated. Jonathan counts - c(rpois(100,5),rpois(100,20)) sites - rep(100,200) fac1 - factor(c(rep(A,100),rep(B,100))) fac2 - factor(c(rep(C,50),rep(D,100),rep(C,50))) model1 - glm(counts ~ fac1 * fac2,family=quasipoisson, offset=log(sites)) model2 - glm(counts ~ fac1 + fac2,family=quasipoisson, offset=log(sites)) anova(model1,model2,test=F) Analysis of Deviance Table Model 1: counts ~ fac1 * fac2 Model 2: counts ~ fac1 + fac2 Resid. Df Resid. Dev Df Deviance F Pr(F) 1 196218.432 2 197219.210 -1 -0.778 0.7134 0.3993 [[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] d.f. in F test of nested glm models
Hi David, Thanks very much, that clears it up for me. I plan to report the result as a typical F-test with numerator and denominator d.f., the value of F and the significance. If you have other thoughts, I would appreciate it. Thanks again. Jonathan On Fri, Nov 26, 2010 at 7:57 PM, David Winsemius dwinsem...@comcast.netwrote: On Nov 26, 2010, at 9:30 PM, Jonathan Flowers wrote: Dear all, I am fitting a glm to count data using poison errors with the log link. My goal is to test for the significance of model terms by calling the anova function on two nested models following the recommendation in Michael Crawley's guide to Statistical Computing. Without going into too much detail, essentially, I have a small overdispersion problem (errors do not fit the poisson assumption) so I am following Crawley's recommendation and setting family=quasipoisson and using an F test (rather than a chi-square test) to test for significance. This is working fine, but I cannot figure out how the F value in the analysis of deviance table was obtained and what degrees of freedom were used to obtain the P value (essentially Numerator df are the absolute values of differences and the denominator df's are the starting point. 1-pf(0.7134, 1, 197) [1] 0.3993472 Or equivalently: pf(0.7134, 1, 196, lower.tail=FALSE) [1] 0.3993472 I don't know how to report the result). Ergo: Time for a statistician. The following example (while errors are not overdispersed) otherwise generates a comparable analysis of deviance table to my analysis. Any help would be much appreciated. Jonathan counts - c(rpois(100,5),rpois(100,20)) sites - rep(100,200) fac1 - factor(c(rep(A,100),rep(B,100))) fac2 - factor(c(rep(C,50),rep(D,100),rep(C,50))) model1 - glm(counts ~ fac1 * fac2,family=quasipoisson, offset=log(sites)) model2 - glm(counts ~ fac1 + fac2,family=quasipoisson, offset=log(sites)) anova(model1,model2,test=F) Analysis of Deviance Table Model 1: counts ~ fac1 * fac2 Model 2: counts ~ fac1 + fac2 Resid. Df Resid. Dev Df Deviance F Pr(F) 1 196218.432 2 197219.210 -1 -0.778 0.7134 0.3993 [[alternative HTML version deleted]] -- David Winsemius, MD West Hartford, CT [[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] graph margins
Use par(oma=c(1,1,1,1)) # oma = outer margin area Is this what your looking for? On Wed, Sep 29, 2010 at 5:03 PM, Mohsen Jafarikia jafari...@gmail.comwrote: Hello All, I am drawing a graph having 18 small graphs inside using par(mfrow = c(6,3)) command. My problem is how to specify the margins of the whole 18 graphs. I used par(mar=c(6.5, 6.5, 1.5, 1.5)) for each graph separately already but it does not left any margins for the 'mtext()' for the margins of the whole 18 graphs. Any comments please. Thanks, MJK [[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. [[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] margin control in lattice package
Hi Peter, Thank you for your thoughtful reply. I am tweaking the setting print settings you suggested. It looks like this is going to solve my problem. Thanks very much for help. Jonathan On Sat, Sep 25, 2010 at 6:00 PM, Peter Ehlers ehl...@ucalgary.ca wrote: On 2010-09-25 8:59, Jonathan Flowers wrote: Hi all, I am difficulty with simple layout of plots in the lattice package I have created a series of levelplots and would like to plot them to a single device, but need to reduce the margin areas. This is easily accomplished with par(oma) and par(mar) in the base graphics package but I am having problems finding the equivalent features in the lattice package. Ideally, I would like to reduce the amount of white space among plots in the following example. Thanks in advance. library(lattice) p1- levelplot( matrix(c(1:25),nr=5,nc=5),row. values=1:5,column.values=1:5) p2- levelplot(matrix (rnorm(25),nr=5,nc=5),row.values=1:5,column.values=1:5) p3- levelplot( matrix(c(1:25),nr=5,nc=5),row.values=1:5,column.values=1:5) p4- levelplot(matrix (rnorm(25),nr=5,nc=5),row.values=1:5,column.values=1:5) print(p1,split=c(1,1,2,2),more=T) print(p2,split=c(2,1,2,2),more=T) print(p3,split=c(1,2,2,2),more=T) print(p4,split=c(2,2,2,2)) Thanks in advance, Jonathan Here are a couple of things you can play with. First, the default for the matrix method of levelplot() is to produce square plots using the argument aspect=iso. So unless you set aspect=some other value or fill, you can only reduce the outer white space at the expense of increasing the inner white space. To reduce the outer white space but keep the aspect=iso, you can set the size of the graphics device and then fiddle with the top.padding and/or bottom.padding components of the layout.heights parameter. Something like this: ## some simple data m - matrix(1:25, nr=5) ## create 4 (identical for illustration only) plots p1 - p2 - p3 - p4 - levelplot(m, aspect=iso, par.settings=list(layout.heights=list(top.padding=-2))) ## open a trellis device (I'm on Windows) trellis.device(windows, height=6, width=7) ## print the plots print(p1, split=c(1,1,2,2), more=TRUE) print(p2, split=c(2,1,2,2), more=TRUE) print(p3, split=c(1,2,2,2), more=TRUE) print(p4, split=c(2,2,2,2)) ## alternatively, use aspect=fill and adjust size in the ## print() calls p1 - p2 - p3 - p4 - levelplot(m, aspect=fill) trellis.device(windows, height=6, width=7) print(p1, split=c(1,1,2,2), panel.height=list(x=2, units=in), more=TRUE) print(p2, split=c(2,1,2,2), panel.height=list(x=2, units=in), more=TRUE) print(p3, split=c(1,2,2,2), panel.height=list(x=2, units=in), more=TRUE) print(p4, split=c(2,2,2,2), panel.height=list(x=2, units=in)) Probably the best way, if your levels are roughly the same for all plots, is to convert your data to a data frame, define a 4-level factor, and create a standard 4-panel plot instead of using the 'split' argument. -Peter Ehlers [[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] margin control in lattice package
Hi all, I am difficulty with simple layout of plots in the lattice package I have created a series of levelplots and would like to plot them to a single device, but need to reduce the margin areas. This is easily accomplished with par(oma) and par(mar) in the base graphics package but I am having problems finding the equivalent features in the lattice package. Ideally, I would like to reduce the amount of white space among plots in the following example. Thanks in advance. library(lattice) p1 - levelplot( matrix(c(1:25),nr=5,nc=5),row. values=1:5,column.values=1:5) p2 - levelplot(matrix (rnorm(25),nr=5,nc=5),row.values=1:5,column.values=1:5) p3 - levelplot( matrix(c(1:25),nr=5,nc=5),row.values=1:5,column.values=1:5) p4 - levelplot(matrix (rnorm(25),nr=5,nc=5),row.values=1:5,column.values=1:5) print(p1,split=c(1,1,2,2),more=T) print(p2,split=c(2,1,2,2),more=T) print(p3,split=c(1,2,2,2),more=T) print(p4,split=c(2,2,2,2)) Thanks in advance, Jonathan [[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] layout of lattice graphics
Hi all, I am trying to pickup lattice graphics in R and having difficulty with simple layout of plots. I have created a series of levelplots and would like to plot them to a single device, but need to reduce the margin areas. This is easily accomplished with par(oma) and par(mar) in the base graphics package but I am having problems finding the equivalent features in the lattice package. Ideally, I would like to reduce the amount of white space among plots in the following example. Thanks in advance. library(lattice) p1 - levelplot( matrix(c(1:25),nr=5,nc=5),row.values=1:5,column.values=1:5) p2 - levelplot(matrix (rnorm(25),nr=5,nc=5),row.values=1:5,column.values=1:5) p3 - levelplot( matrix(c(1:25),nr=5,nc=5),row.values=1:5,column.values=1:5) p4 - levelplot(matrix (rnorm(25),nr=5,nc=5),row.values=1:5,column.values=1:5) print(p1,split=c(1,1,2,2),more=T) print(p2,split=c(2,1,2,2),more=T) print(p3,split=c(1,2,2,2),more=T) print(p4,split=c(2,2,2,2)) Best Jonathan [[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] Scatterplot_row match
Hi, What if you create two data frames, then merge them by gene id. If your data is in a data frame called dframe... df1 - subset(dframe,select=c(id2c,2c)) df2 - subset(dframe,select=c(id1c,1c)) merged - merge(df1,df2,by.x=id2c,by.y=id1c,all=TRUE) plot(merged$1c,merged$2c) Cheers, Jonathan On Thu, Jul 15, 2010 at 5:12 AM, Yanwei Tan t...@nbio.uni-heidelberg.dewrote: Dear all, I would like to make a scatter plot using plot function. I have two sample 1c and 2c, the 1st and 3rd are the ID of each gene, the 2nd and 3rd are the values. But as you can see some genes are not in the same row, i.e: ENSMUSG0001020 is in the 4th row in sample 2c and 7th row of the sample 1c. But I have nearly 1000 genes, I can not edit the data manually to fit the plot function. Does Plot can automatically recognize every gene in different row of samples? If I simply do plot(2c,1c), then I guess the dot about gene ENSMUSG0001020 would be (0.122112211, 0.655010678 ) instead of (0.122112211, 0.15301479). Can someone give me some hint? With many thanks in advance! Best wishes, Wei id2c2c id1c1c ENSMUSG184 0.345889139 ENSMUSG184 0.601580659 ENSMUSG202 0.310589755 ENSMUSG202 0.422453875 ENSMUSG531 1.945122637 ENSMUSG355 0.633385261 ENSMUSG0001020 0.122112211 ENSMUSG567 0.655010678 ENSMUSG0001228 0.157001414 ENSMUSG948 0.785187729 ENSMUSG0001403 0 ENSMUSG957 0.643804965 ENSMUSG0001435 1.865292034 ENSMUSG0001020 0.15301479 ENSMUSG0001473 2.156209643 ENSMUSG0001025 0.43609215 ENSMUSG0001508 3.736633663 ENSMUSG0001228 0.087437023 ENSMUSG0001604 3.846534653 ENSMUSG0001403 0.045904437 ENSMUSG0001655 NA ENSMUSG0001435 1.3497589 ENSMUSG0001773 0.10990099 ENSMUSG0001506 0.688566553 ENSMUSG0001864 0.212711594 ENSMUSG0001555 0.571640157 ENSMUSG0002055 0.224287735 ENSMUSG0002055 0.393466602 ENSMUSG0002076 13.18811881 ENSMUSG0002076 13.77133106 ENSMUSG0002274 0.318134445 ENSMUSG0002265 0.659467911 [[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. [[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] select columns from vector of column names
Hi I want to extract columns from a data frame using a vector with the desired column names. This short example uses the select argument in the subset function to accomplish what I am trying to do. Is there a better solution? #names of desired columns colnames - c(col1,col3) #my data data - data.frame(col1=c(1,2,3),col2=c(A,B,C),col3=c(4,5,6)) fun - function(colname,dframe){ nframe - subset(dframe,select=colname) vec - nframe[,1] return(vec) } fun(colnames[1],data) fun(colnames[2],data) [[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] select columns from dataframe
Hi, I would like to extract columns from a dataframe using a vector of desired column names. The following working example uses the select argument in the subset function to accomplish what I am trying to do. Is there a better solution? Thanks. #my data data - data.frame(col1=c(1,2,3),col2=c(A,B,C),col3=c(4,5,6)) #names of desired columns colnames - c(col1,col3) fun - function(colname,dframe){ nframe - subset(dframe,select=colname) vec - nframe[,1] return(vec) } fun(colnames[1],data) fun(colnames[2],data) [[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.