本文介绍了R 等效于 .first 或 .last sas 运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁首先知道什么是 SAS 的最佳 R 替代方案.或最后.运营商?我没有找到.

Does anybody know what is the best R alternative to SAS first. or last. operators? I did find none.

SAS 是第一个.和最后.自动变量,用于识别与特定变量具有相同值的组中的第一条和最后一条记录;因此在以下数据集中定义了 FIRST.model 和 LAST.model:

SAS has the FIRST. and LAST. automatic variables, which identify the first and last record amongst a group with the same value with a particular variable; so in the following dataset FIRST.model and LAST.model are defined:

Model,SaleID,First.Model,Last.Model
Explorer,1,1,0
Explorer,2,0,0
Explorer,3,0,0
Explorer,4,0,1
Civic,5,1,0
Civic,6,0,0
Civic,7,0,1

推荐答案

听起来您正在寻找 !duplicatedfromLast 参数是 FALSETRUE.

It sounds like you're looking for !duplicated, with the fromLast argument being FALSE or TRUE.

d <- datasets::Puromycin

d$state
# [1] treated   treated   treated   treated   treated   treated   treated
# [8] treated   treated   treated   treated   treated   untreated untreated
#[15] untreated untreated untreated untreated untreated untreated untreated
#[22] untreated untreated
#Levels: treated untreated
!duplicated(d$state)
# [1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#[13]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
!duplicated(d$state,fromLast=TRUE)
# [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
#[13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE

此函数有一些注意事项和边界情况行为,您可以通过帮助文件 (?duplicated) 找到这些信息.

There are some caveats and edge-case behaviors to this function, which you can find out through the help files (?duplicated).

这篇关于R 等效于 .first 或 .last sas 运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 22:16
查看更多