Skip to content

why data frame's logical index isnt working

5 messages · Richard M. Heiberger, Michael Artz, David Winsemius

#
data.frame.$columnToAdd["CurrentColumnName" == "ConditionMet"] <- 1

Can someone please explain to me why the above command gives all NAs to
columnToAdd?  I thought this was possible in R to do logical expression in
the index of a data frame
#
you probably mean something like this

data.frame.$columnToAdd <- (data.frame.$CurrentColumnName ==
data.frame.$ConditionMet)

what you did is compare two character strings.  They are not the same.
Therefore a new column
is created with the default value NA.
A B
1 1 1
2 2 3
3 3 4
4 4 5
A B     C
1 1 1  TRUE
2 2 3 FALSE
3 3 4 FALSE
4 4 5 FALSE
A B     C  D
1 1 1  TRUE NA
2 2 3 FALSE NA
3 3 4 FALSE NA
4 4 5 FALSE NA

        
On Thu, Apr 7, 2016 at 9:46 PM, Michael Artz <michaeleartz at gmail.com> wrote:
#
It is possible, but please execute this at a console line and then read ?"[" to see what is happening:

"CurrentColumnName" == "ConditionMet"  # almost surely FALSE

Let's assume your dataframe were named 'dat'.

Perhaps you meant to write:

dat$colToAdd[ dat[["CurrentColumnName"]] == dat[["ConditionMet"]] ] <- 1

And do please stop naming your dataframes "data.frame".
David Winsemius
Alameda, CA, USA
#
I don't get it, I thought the double index was to indicate and individual
element within a column(vector)?
I will stop using data.frame, thanks a lot!

On Thu, Apr 7, 2016 at 9:29 PM, David Winsemius <dwinsemius at comcast.net>
wrote:

  
  
#
Character values by themselves either quoted or not are not assumed to refer to column names unless you use `with` or `within`.
You will have a lot of problems using R if you stop using data.frames.