[R] operator problem within function

David Winsemius dwinsemius at comcast.net
Thu Apr 29 00:45:14 CEST 2010


On Apr 28, 2010, at 5:31 PM, Bunny, lautloscrew.com wrote:

> Dear all,
>
> i have a problem with processing dataframes within a function using  
> the "$".
> Here´s my code:
>
>
> recode_items = function(dataframe,number,medium=2){
> 	
> 	# this works
> 	q<-paste("columna",number,sep="")

Do your really want q to equal "columna2" when "number" equals 2?

>
> 	# this does not work, particularly because "dataframe" is not  
> processed
> 	# dataframe should be: givenframe$columnagivennumber
> 	a=dataframe$q[dataframe$q==medium]=1

a) Do you want to work on the column from dataframe ( horrible name  
for this purpose IMO) with the name "columna2"? If so, then start with

dataframe[ , q ]

.... the "q" will be evaluated in this form whereas it would not when  
used with "$".

b) (A guess in absence of explanation of a goal.) Now do you want all  
of the rows where that vector equals "medium"? If so ,then try this:

dataframe[ dataframe[ , q ]==2 , ]  # untested in the absence of data

Moral: Generally better to use "[" indexing.

-- 
David.


>
>
> 	return(a)	
>
> }
>
>
> If I call this function, i´d like it to return my  dataframe.  The  
> problem appears to be somewhere around the $. I´m sure this not too  
> hard, but somehow i am stuck. I´ll keep searchin the manuals.
> Thx for any help in advance.
>
> best
>
> matt
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.



More information about the R-help mailing list