[英]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_distinct
或apply
族函数。 It has columns that aren't numerical
or integer
它的列不是
numerical
或integer
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.