R Not in Subset

Opposite of %in%: exclude rows with values specified in a vector

You can use the ! operator to basically make any TRUE FALSE and every FALSE TRUE. so:

D2 = subset(D1, !(V1 %in% c('B','N','T')))

EDIT:
You can also make an operator yourself:

'%!in%' <- function(x,y)!('%in%'(x,y))

c(1,3,11)%!in%1:10
[1] FALSE FALSE TRUE

R Not in subset

The expression df1$id %in% idNums1 produces a logical vector. To negate it, you need to negate the whole vector:

!(df1$id %in% idNums1)

Subset values that are not in the other dataset

The following gives the missing values :

data.imp[is.na(data.org)]
#[1] 10 25 30 55 15 40

To have these values column-wise we can use Map :

Map(function(x, y) y[is.na(x)], data.org, data.imp)

#$WT
#[1] 10 25

#$HT
#[1] 30 55

#$CBC
#[1] 15 40

filtering or subsetting a dataframe does not include all values

We can use %in% instead of == as == is elementwise operator and can only with a single element or the lengths shsould be same on the lhs and rhs of ==

library(dplyr)
df %>%
dplyr::filter(ID %in% c(1,2,5,7,9))

Subset a vector and retrieve first elements if exceed the length in R

I think I got it!

v <- c("A", "B", "C", "D")
p <- sample(1:length(v), 1)
r <- c(v[p:length(v)])
c(r, v[!(v %in% r)])[1:3]

And the outputs:

v <- c("A", "B", "C", "D") # your vector

r <- c(v[2:length(v)])
c(r, v[!(v %in% r)])[1:3]
#> [1] "B" "C" "D"

r <- c(v[3:length(v)])
c(r, v[!(v %in% r)])[1:3]
#> [1] "C" "D" "A"

r <- c(v[4:length(v)])
c(r, v[!(v %in% r)])[1:3]
#> [1] "D" "A" "B"

Created on 2022-05-16 by the reprex package (v2.0.1)

Wrapped in a function:

f <- function(v, nth) {
r <- c(v[nth:length(v)])
return(c(r, v[!(v %in% r)])[1:3])
}

Subset function in R works on one column but not on the other

Let's show what should have been done:

 df2 <- select(df, PLAYER_NAME, GP, PLUS_MINUS)

str(df2) # notice that GP displays like 'numeric' but is really 'factor'
#--------------
'data.frame': 382 obs. of 3 variables:
$ PLAYER_NAME: Factor w/ 382 levels "Aaron Gordon",..: 1 2 3 4 5 6 7 8 9 10 ...
$ GP : Factor w/ 23 levels "1","10","11",..: 22 22 12 12 6 1 21 1 2 12 ...
$ PLUS_MINUS : Factor w/ 89 levels "-0.1","-0.2",..: 53 63 33 29 3 39 14 86 78 10 ...
#-----------------------
df2$GP <- as.numeric(as.character(df2$GP)) #convert factor to numeric
df3 <- subset(df2, GP > 10)
str(df3)
#----------------------
'data.frame': 157 obs. of 3 variables:
$ PLAYER_NAME: Factor w/ 382 levels "Aaron Gordon",..: 5 13 14 16 17 21 23 29 31 32 ...
$ GP : num 14 18 16 11 14 18 18 13 17 12 ...
$ PLUS_MINUS : Factor w/ 89 levels "-0.1","-0.2",..: 3 5 48 76 33 11 66 43 10 3 ...
#----------

If this had been the result of a read.table or other read.* operation then depending on your version of R the GP column might have been either factor as it was for me in my 3.6.2 session or character as it might have been for anyone using an up-to-date version. The default for stringsAsFactors was changed in the transition to version 4 and above. When it is a factor, the GP column would first needs to be converted to character before it can be converted to numeric. In your case it might be that jsonlite has not yet made the same decision about assuming columns that can be numeric should be numeric.

If you are running R 4.0+ or above, you don't need the as.character inside the as.numeric.

How to check whether a vector is subset of another one in R?

You can test if length is == and if any values of A have a 1 on positions where B has a 1 and combine the conditions with &&.

length(A) == length(B) && any(A[B==1]==1)
#[1] TRUE

Fulfilling the condition in the original question: A and B have the same length and thus have the same number of elements; yet, A is a subset of B, since A has elements 1 in the same place as B.

To fulfill:

  • A and B have the same length and thus have the same number of elements;
  • A should have at least one 1 at places wherever B has 1
  • A can have only 0s at all places where B has 0s.
length(A) == length(B) && any(A[B==1]==1) && all(A[B==0]==0)

To fulfill:

  • A and B have the same length and thus have the same number of elements;
  • A can have only 0s at all places where B has 0s.
length(A) == length(B) && all(A[B==0]==0)


Related Topics



Leave a reply



Submit