How to get "who is different" in vector, with R

Simple question. Consider this vector:

[1] 378 380 380 380 380 360 187 380 

How can we determine which numbers differ from the others on this list? In that case, it will be 378,360 and 187. Any ideas? I know that a solution may not be easy ...

I study R and work on a dataset for my research, so this is! = Homework.

Any help would be greatly appreciated!

+4
source share
4 answers

You can find the most frequent record using table() and which.max() , then you can index the original vector with a logical vector containing unequal records, for example: data [data! = Mostfrequent]. You can get help ?table() and ?which.max() , comment if you need more.

Your sample vector

 x <- c(378, 380, 380, 380, 380, 360, 187, 380) 

Find the frequency of each number in it using table . For convenience, in the future we will convert it to a data frame.

 counts <- as.data.frame(table(x), stringsAsFactors = FALSE) 

which.max allows us to find the modal value (the most common).

 modal_value <- which.max(counts$Freq) 

Other values ​​can then be found using indexing.

 as.numeric(counts[-modal_value, "x"]) 
+2
source

Perhaps another alternative:

 x <- c(378, 380, 380, 380, 380, 360, 187, 380) setdiff(unique(x), x[duplicated(x)]) 
+4
source

The extraction of non-repeated elements can be performed using:

 a<-c(378, 380, 380, 380, 380, 360, 187, 380) b <- table(a) names(b[b==1]) #[1] "187" "360" "378" 
+3
source

Another approach:

 x <- c(378, 380, 380, 380, 380, 360, 187, 380) y <- rle(sort(x)); y[[2]][y[[1]]==1] 
+3
source

Source: https://habr.com/ru/post/1396972/


All Articles