[R] how to use a column name from the data frame in the function

2013-08-22 Thread song song
for example I have data frame m as below: m=as.data.frame(outer(1:5,6:9)) colnames(m)=c('a','b','c','d') and I define the function myf=function(df, colname){ suppose colname is a, then: how can I get the column 'a' and how to get the colname as a string, 'a' } Thank you!

[R] how to use a column name from the data frame in the function

2013-08-22 Thread song song
m=as.data.frame(outer(1:5,6:9)) colnames(m)=c('a','b','c','d') tf=function(df, col){list(mean(eval(substitute(col),df,parent.frame())),col )} tf(m,a) will issue error: Error in tf(m, a) : object 'a' not found How can I replace the col as char 'a' in the function? Thank you

[R] how to compare two datasets in R?

2010-11-02 Thread song song
hi, everybody, my question is: suppose I have two data sets, set A is large and have variables like ID, Gender, Income. Set B is small and suppose only has ID. Now I want to get a subset from data set A which contains ID from Set B. How to do this in R? Is there any commands to do this?

[R] how to to if a calculation is out range?

2010-09-29 Thread song song
for example, when I am calculating a posterior density, I need to calculate gamma(75*3+5)=gamma(220) which is out of the bound of gamma function. what shall I do for this condition Thank you [[alternative HTML version deleted]] __

[R] if using ginv function, does it mean there is no need to use solve function any more?

2010-07-04 Thread song song
since ginv can deal with both singular and non-singular conditions, is there any other difference between them? if I use ginv only, will be any problem? thanks [[alternative HTML version deleted]] __ R-help@r-project.org mailing list

[R] ask a question about list in R project

2010-06-28 Thread song song
my list al is as below: mylist=list(c(2,3),5,7) mylist [[1]] [1] 2 3 [[2]] [1] 5 [[3]] [1] 7 How could I get the following FOUR lists: First one [[1]] [1] 3 [[2]] [1] 5 [[3]] [1] 7 Second one [[1]] [1] 2 [[2]] [1] 5 [[3]] [1] 7 Third One [[1]] [1] 2 3 [[2]] [1] 7 Last one [[1]] [1] 2

[R] how to remove numeric(0) component from a list

2010-06-28 Thread song song
like this, the list is below, I want to remove the last one . not using newlist[-2], but using the function detect its component is numeric(0) and then remove it from the list. newlist [[1]] [1] 2 3 [[2]] [1] numeric(0) [[3]] [1] 7 [[alternative HTML version deleted]]

Re: [R] ask a question about list in R project

2010-06-28 Thread song song
I found this loop can do this. is there any simple method? rm(list=ls()) a=c(2,3,5,7) mylist=list(c(2,3),5,7) newlist=list() for (i in 1:4){ for (j in 1:length(mylist)){ newlist[[j]]=mylist[[j]] if (a[i] %in% mylist[[j]]){ newlist[[j]]=mylist[[j]][mylist[[j]]!=a[i]]

[R] ask a question about list in R

2010-06-24 Thread song song
my list al is as below: al=list(c(2,3),5,7) al [[1]] [1] 2 3 [[2]] [1] 5 [[3]] [1] 7 and I check the second component, its element is 5, then I remove this, now my al is: al[[2]][al[[2]]!=5]-al[[2]] al [[1]] [1] 2 3 [[2]] numeric(0) [[3]] [1] 7 The Question is, how I can get the new list

[R] how to initial a list to store data result?

2010-06-21 Thread song song
May I ask how to initialize a list? usually I will use result=list(0) to do this. is this right? [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the

[R] ask a question about data sets element

2010-06-17 Thread song song
for example, I have a1=c(1,3,5) a2=c(2,4,6) a3=c(7,8) a4=c(9,10) now if I have i=5, so i in a1, then I get a feedback tag[5]=1 i=8, so i in a3, then can get tag[8]=3 in there any function to do this to check the element belongs to which group? thank you!!!

[R] how to rewrite this for loops in matrix form without loop

2010-05-03 Thread song song
x0=rnorm(100) y0=rpois(100,3)+1 ind=as.data.frame(table(y0)) ind1=ind[,1] ind2=ind[,2] phi=NULL for (i in 1:length(ind2)){ phi[i]=sum(x0[y0==ind1[i]])/ind2[i] } [[alternative HTML version deleted]] __ R-help@r-project.org mailing list

[R] Is it possible to transform a factor to a number like this way?

2010-04-29 Thread song song
such a factor as a=as.factor(c(1.23, 4.56, 7.89)) and I want to get this vector: c(1.23, 4.56, 7.89). Thanks [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do

[R] how to remove the names in the list result

2010-04-28 Thread song song
hi,everybody: I get a list as following. For each element of the list, it has names such as Var1 and Freq. May I ask how to remove them? And, may I ask how to make them form a matrix such as like concatinating them? Thank you! The list I got: [[1]] Var1 Freq 1

[R] How to store an array like this?

2010-02-19 Thread song song
maybe its not an array like m[1]=matrix(1:4,2,2) m[2]=matrix(1:9,3,3) . . . m[k]=matrix(1:k^2,k,k) likes for (k in 1:n){ s=matrix(1:k^2,k,k) } how to store s like s[1], s[2] ... so that I can use it in other place? thanks! [[alternative HTML version deleted]]

[R] how to read this data file into R?

2010-02-02 Thread song song
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 2.010061 -5.223624 2.010061 2.010061 1.463336 1.463336 2.010061 2.010061 [2,] 2.057502 -5.101445 2.057502 2.057502 2.057502 2.057502 2.057502 2.057502 [3,] 2.011081 -5.227745 2.011081 2.011081 2.011081 2.011081

[R] ask a question about data replacement

2010-01-28 Thread song song
the question like this : x=c(2,6,8,11,11,11,6,8,2,8,6,11,8,2,6,11) x contains values (2, 6, 8, 11) with frequency (3, 4, 4, 5). now I want to get the vector by replacing (2, 6, 8, 11) --- (1 2 3 4) that is, want to get new x as (1 2 3 4 4 4 2 3 1 3 2 4 3 1 2 4) is there any function in R

Re: [R] ask a question about data replacement

2010-01-28 Thread song song
i got it use function rank can get the result 2010/1/28 song song rprojecth...@gmail.com the question like this : x=c(2,6,8,11,11,11,6,8,2,8,6,11,8,2,6,11) x contains values (2, 6, 8, 11) with frequency (3, 4, 4, 5). now I want to get the vector by replacing (2, 6, 8, 11) --- (1 2 3 4

[R] ask for help with this R quesiton

2010-01-27 Thread song song
question like this: I have data tag and phi, when tag are the same, then phi are the same. like below, tag have value 1 2 3 4 and phi have value .9 .3 1 0. my question is, how can I get the result like tag=c(1,2,3,4) and corresponding phi=c(.9,.3,1,0) tag phi 2.3 1.9 2.3

[R] ask for help with this R quesiton

2010-01-27 Thread song song
question like this: I have data tag and phi, when tag are the same, then phi are the same. like below, tag have value 1 2 3 4 and phi have value .9 .3 1 0. my question is, how can I get the result like tag=c(1,2,3,4) and corresponding phi=c(.9,.3,1,0) tag phi 2.3 1.9 2.3 31 2

Re: [R] ask for help with this R quesiton

2010-01-27 Thread song song
question like this: I have data tag and phi, when tag are the same, then phi are the same. like below, tag have value 1 2 3 4 and phi have value .9 .3 1 0. my question is, how can I get the result like tag=c(1,2,3,4) and corresponding phi=c(.9,.3,1,0) tag phi 2.3 1.9 2.3