如何计算R中一行中的唯一值
我在R中有以下面板数据集,其中包含一个ID变量,并显示该ID的上次登录详细信息如何计算R中一行中的唯一值,r,unique-values,R,Unique Values,我在R中有以下面板数据集,其中包含一个ID变量,并显示该ID的上次登录详细信息 id name address last_log_june1 last_log_june2 last_log_june3 last_log_june4 last_log_june"n" 1 A 2020-06-01 2020-06-01 2020-06-03 2 B 2020-06-01 2020-06-01 2020-06-01 3
id name address last_log_june1 last_log_june2 last_log_june3 last_log_june4 last_log_june"n"
1 A 2020-06-01 2020-06-01 2020-06-03
2 B 2020-06-01 2020-06-01 2020-06-01
3 C 2020-06-01 2020-06-02 2020-06-03
在上面的数据集中,我想计算A、B和C登录的唯一次数。如何在R中做到这一点,以便我只选择最后的日志日期变量,并让R计算其中的唯一日期?我还想将此计数列添加到数据集中
期待着解决这个问题
谢谢,
Rachita您需要独特的函数,并将其应用于行
dfDP1.0.0版软件包中的一些函数可能会有所帮助 假设您的数据名为df,其中包含列ID、name、address和一系列以last_log_june开头的列,这些列中可能存在一些NA值
new_df <- df %>% rowwise() %>% ## indicate you want to apply functions on rows
mutate(na_exists = ifelse(sum(is.na(c_across(starts_with("last_log_june"))))>0,1,0),
## an intermediate variable na_exists to indicate whether or not there is `NA` in any of the columns
unique_with_NA = length(unique(c_across(starts_with("last_log_june")),na.rm=T))
## if there is NA, the unique function will also count `NA` as a unique value
unique_withno_NA = unique_with_NA-na_exists
## if you don't want NA counted as an unique value, then the final result should exclude it
) %>% select (-na_exists, -unique_with_NA)
## remove the intermediate variables
使用函数CyAcRasSaltStIsLaStasLogLogy6只考虑列从LaSTyLogLog[6]/P>熔体到长格式和摘要::PIVoToLangErdAT,C-ID,-No.NAMESYTO=变量,ValuthSoto=DATES,非常感谢,项!我觉得这应该能奏效。然而,我得到一个错误,c_必须只在dplyr动词中使用。尽管我已经安装了声明版本的dplyr包。你知道如何克服这个问题吗?谢谢也许您可以尝试在代码中使用dplyr::c_over,这指定c_over函数来自dplyr包。由于c_Cross仅在dplyr 1.0.0中起作用,您可以在控制台中运行sessionInfo以再次检查其版本是否正确。然而,它仍在向我展示上述错误。非常感谢您的回复和帮助!