简体   繁体   English

如何使用基础 R 中的 for 循环计算唯一值的数量?

[英]How to compute the number of unique values with a for-loop in base R?

I have a big data frame and would like to count the number of unique values per column using a for-loop and base R - so no n_distinct or apply family functions.我有一个大data frame ,想使用for-loop和基本 R 来计算每列的unique值的数量 - 所以没有n_distinctapply族函数。 It has columns that aren't numerical or integer它的列不是numericalinteger

If we need a for loop - initialize a list to store the output, then loop over the sequence of columns, extract the column ( df1[[i]] ), get the unique values, find the length and store that in the corresponding list element.如果我们需要一个for循环 - 初始化一个list来存储输出,然后循环列序列,提取列( df1[[i]] ),获取unique值,找到length并将其存储在相应的列表中元素。 Note that list is just one way to store the object in case if the object is complex.请注意,如果对象很复杂, list只是存储对象的一种方式。 Otherwise, we can also do out <- integer(ncol(df)) as initialization which returns a integer vector否则,我们也可以out <- integer(ncol(df))作为初始化,返回一个整数向量

out <- vector('list', ncol(df1))
for(i in seq_along(df1))
  out[[i]] <- length(unique(df1[[i]]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM