[R] why data frame's logical index isnt working

Richard M. Heiberger rmh at temple.edu
Fri Apr 8 03:54:53 CEST 2016


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.

> tmp <- data.frame(A=1:4, B=c(1,3,4,5))
> tmp
  A B
1 1 1
2 2 3
3 3 4
4 4 5
> tmp$C <- tmp$A == tmp$B
> tmp
  A B     C
1 1 1  TRUE
2 2 3 FALSE
3 3 4 FALSE
4 4 5 FALSE
> tmp$D["A" == "B"] <- 1
> tmp
  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:
> 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
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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