[R] operator problem within function
David Winsemius
dwinsemius at comcast.net
Thu Apr 29 15:25:19 CEST 2010
On Apr 29, 2010, at 9:03 AM, Bunny, lautloscrew.com wrote:
> Sorry for that offlist post, did not mean to do it intentionally.
> just hit the wrong button. Unfortunately this disadvantage is not
> written next to $ in the manual.
Hmmm. Not my manual:
"Both [[ and $ select a single element of the list. The main
difference is that $ does not allow computed indices, whereas [[does."
It also says that the correct equivalent using extraction operators of
"$" would be:
x$name == x[["name", exact = FALSE]]
--
David.
>
>
>>
>> On Apr 29, 2010, at 2:34 AM, Bunny, lautloscrew.com wrote:
>>
>>> David,
>>>
>>> With your help i finally got it. THX!
>>> sorry for handing out some ugly names.
>>> Reason being: it´s a german dataset with german variable names.
>>> With those german names you are always sure you dont use a forbidden
>>> name. I just did not want to hit one of those by accident when
>>> changing these names for the mailing list. columna is just the
>>> latin term for column :) . Anyway here´s what worked
>>>
>>> note: I just tried to use some more "real" names here.
>>>
>>> recode_items = function(dataframe,question_number,medium=3){
>>>
>>> #note column names of the initial data.frame are like
>>> Question1,Question2 etc. Using [,1] would not be very practical
>>> since # the df contains some other data too. Indexing by names
>>> seemed to most comfortable way so far.
>>> question<-paste("Question",question_number,sep="")
>>> # needed indexing here that understands characters, that´s why
>>> going with [,question_number] did not work.
>>> dataframe[question][dataframe[question]==3]=0
>>
>> This would be more typical:
>>
>> dataframe[dataframe[question]==3, question] <- 0
>>
>>>
>>>
>>> return(dataframe)
>>>
>>> }
>>>
>>> recode_items(mydataframe,question_number,3)
>>> # this call uses the dataframe that contains the answers of survey
>>> participants. Question number is an argument that selects the
>>> question from the dataframe that should be recoded. In surveys
>>> some weighting schemes only respect extreme answers, which is why
>>> the medium answer is recoded to zero. Since it depends on the item
>>> scale what medium actually is, I need it to be an argument of my
>>> function.
>>>
>>>> Did you want a further logical test with that "=1" or some sort
>>>> of assignment???
>>>
>>> So yes, it´s an assignment.
>>>
>>>>> Moral: Generally better to use "[" indexing.
>>>
>>> That´s what really made my day (and it´s only 9.30 a.m. here ) .
>>> Are there exceptions to rule?
>>
>> Not that I know of.
>>
>>> I just worked a lot with the $ in the past.
>>
>> "$colname" is just syntactic sugar for either "["colname"]" or
>> "[ ,"colname"]" and it has the disadvantage that colname is not
>> evaluated.
>>
>>
>>>
>>> thx
>>>
>>> matt
>>>
>>>
>>>
>>>
>>> On 29.04.2010, at 00:56, David Winsemius wrote:
>>>
>>>>
>>>> On Apr 28, 2010, at 5:45 PM, David Winsemius wrote:
>>>>
>>>>>
>>>>> 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
>>>>
>>>> Did you want a further logical test with that "=1" or some sort
>>>> of assignment???
>>>>
>>>>>
>>>>> 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
>>>>
>>>> Ooops. should have been:
>>>>
>>>> dataframe[ dataframe[ , q ]==medium , ] #since both q and medium
>>>> will be evaluated.
>>>>
>>>>
>>>>>
>>>>> 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.
>>>>>
>>>>> ______________________________________________
>>>>> 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.
>>>>
>>>
>>
>
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list