Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201919

One way among different expressions of "subset()" function failed in R

$
0
0

I found in the Help file of subset() that different syntax occur for the "selection" or "inverse selection". Specifically, in "selection" example, the columns's name are surrounded by c(), but in "inverse selection", there is no c(). In addition, they both don't have quatation mark. So I tried some different syntax:

## For `selection` 
# standard usage in Help
subset(airquality, select = c(Temp))

# some different syntax
# (1a) add quotation mark: worked
subset(airquality, select = c("Temp"))
# (2a) remove "c()": worked
subset(airquality, select = Temp)

## For `inverse selection` 
# standard usage in Help
subset(airquality, select = -Temp)

# some different syntax
# (1b) add "c()": worked
subset(airquality, select = -c(Temp))
# (2b) add quotation mark: failed
#     Error in -c("Temp") : invalid argument to unary operator
subset(airquality, select = -c("Temp"))

These non-standard usage all worked, except the last example. The last example generated an error: Error in -c("Temp") : invalid argument to unary operator

I have two questions:

(1) Why does this error occur? Because in (1a) and (2b), these two non-standard usage seems the same, but (1a) succeeded, while (2b) failed.

(2) Could someone tell me when should I use c(), or "" in c()?


Viewing all articles
Browse latest Browse all 201919

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>