1. Which function in R

      [This article was first published on R – Greetz to Geeks, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
      Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

      The which() function will return the position of the elements(i.e., row number/column number/array index) in a logical vector which are TRUE. Unlike the other base R functions, the which() will accept only the arguments with typeof as logical while the others will give an error.

      Syntax: which(x, arr.ind = FALSE, useNames = TRUE)
                       arrayInd(ind, .dim, .dimnames = NULL, useNames = FALSE)

      x: can be a logical vector or an array. NAs are treated as FALSE and  allowed. But they will be omitted
      arr.ind: logical; should array indices be returned when x is an array?
      ind: integer-valued index vector, as resulting from which(x)
      .dim: integer vector
      .dimnames: optional list of character dimnames(.), of which only .dimnames[[1]] is used
      useNames: logical indicating if the value of arrayInd() should have (non-null) dimnames at all

      Examples:

      > which(letters == "g")[1] 7
      > x <- c(1,5,8,4,6)> x[1] 1 5 8 4 6> which(x == 5)[1] 2> which(x != 5)[1] 1 3 4 5
      > datasets::npkblock N P K yield1   1  0 1 1 49.52   1  1 1 0 62.83   1  0 0 0 46.84   1  1 0 1 57.05   2  1 0 0 59.86   2  1 1 1 58.57   2  0 0 1 55.58   2  0 1 0 56.09   3  0 1 0 62.810  3  1 1 1 55.811  3  1 0 0 69.512  3  0 0 1 55.013  4  1 0 0 62.014  4  1 1 1 48.815  4  0 0 1 45.516  4  0 1 0 44.217  5  1 1 0 52.018  5  0 0 0 51.519  5  1 0 1 49.820  5  0 1 1 48.821  6  1 0 1 57.222  6  1 1 0 59.023  6  0 1 1 53.224  6  0 0 0 56.0> which(npk$yield == 62)[1] 13> which((npk$yield == 48.8) & (npk$N == 1))[1] 14which(npk$yield == max(npk$yield))[1] 11
      > x <- matrix(1:16,4,4)> x[,1] [,2] [,3] [,4][1,]  1    5   9    13[2,]  2    6  10    14[3,]  3    7  11    15[4,]  4    8  12    16> which.min(x)[1] 1> which.max(x)[1] 16> which(x %% 2 == 0)[1] 2 4 6 8 10 12 14 16> which(x %% 2 == 0, arr.ind=TRUE)row col[1,] 2 1[2,] 4 1[3,] 2 2[4,] 4 2[5,] 2 3[6,] 4 3[7,] 2 4[8,] 4 4> which(x %% 2 == 0, arr.ind=TRUE, useNames = FALSE)[,1] [,2][1,] 2   1[2,] 4   1[3,] 2   2[4,] 4   2[5,] 2   3[6,] 4   3[7,] 2   4[8,] 4   4

       

      To leave a comment for the author, please follow the link and comment on their blog: R – Greetz to Geeks.

      R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
      Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

      Never miss an update!
      Subscribe to R-bloggers to receive
      e-mails with the latest R posts.
      (You will not see this message again.)

      Click here to close (This popup will not appear again)

          1. http://www.ri2bj2r4.tw | http://m.ri2bj2r4.tw | http://wap.ri2bj2r4.tw | http://3g.ri2bj2r4.tw | http://4g.ri2bj2r4.tw | http://5g.ri2bj2r4.tw | http://mobile.ri2bj2r4.tw | http://vip.ri2bj2r4.tw | http://ios.ri2bj2r4.tw | http://anzhuo.ri2bj2r4.tw | http://e7e6b6.ri2bj2r4.tw | http://989c.ri2bj2r4.tw | http://83230.ri2bj2r4.tw | http://80.ri2bj2r4.tw | http://ebdd2.ri2bj2r4.tw | http://b0c9c6.ri2bj2r4.tw | 我能吸你奶吗在线阅读