我有这些代码,生成随机值作为序列的一部分。我需要保持重复,并删除不重复的值。有什么帮助吗?
显然,一个解决方案应该包含"%/%",如。数字,名称,表格,>'
这是原始代码。
x & lt; -样本(c(10:10),样品(20:40,1),取代= TRUE)
任何帮助将不胜感激!
我将使用table()。表将给出所有值的列表以及它们出现的次数。
vec <- c(1,2,3,4,5,3,4,5,4,5)
valueset <- unique(vec) # all unique values in vec, will be used later
#now we determine whihc values are occuring more than once
valuecounts <- table(vec) # returns count of all unique values, values are in the names of this variable as strings now regardless of original data type
morethanone <- names(valuecounts)[valuecounts>1] #returns values with count>1
morethanone <- as.numeric(morethanone) # converts strings back to numeric
valueset[valueset %in% morethanone] #returns the original values which passed the above cirteria
作为函数....
duplicates <- function(vector){
# Returns all unique values that occur more than once in a list
valueset <- unique(vector)
valuecounts <- table(vector)
morethanone <- names(valuecounts)[valuecounts>1]
morethanone <- as.numeric(morethanone)
return( valueset[valueset %in% morethanone] )
}
现在试着运行duplicates(x)