'Does subsetting in R with brackets act inconsistently, or am I missing something?

I'm stumped. With two different datasets, square brackets are returning different types of results and causing problems. In one case, the output is a list, and in the other, it's a set of values (is there another term?).

ds1 = dataset1; ds2 = dataset 2

typeof(ds1$col)  
[1] "integer"
mean(ds1$col)
[1] 0.51
mean(ds1[ds1$col2==1,'col'])
[1] 0.52


typeof(ds2$col)
[1] "double"
mean(ds2$col)
[1] 0.53
mean(ds2[ds2$col2==1,'col'])
[1] NA
Warning message:
In mean.default(cmv2[cmv2$post_num == 1, "accurate"]) :
  argument is not numeric or logical: returning NA

This was how I discovered the problem. To see what was up with the output, I did the following:

> ds1[ds1$col2==1,'col']
[1] 1 1 0 0 0 0



> ds2[ds2$col2==1,'col']
# A tibble: 19 × 1
   accurate
      <dbl>
 1        1
 2        0
 3        0

I don't want the output to be a list. What do I do? Thanks!

r


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source