【问题标题】:How do you mark unique occurrences in a pattern given that value are unique when occurring simultaneously and not when they come separately? [closed]鉴于值在同时出现而不是单独出现时是唯一的,你如何标记模式中的唯一出现? [关闭]
【发布时间】:2015-09-29 08:07:38
【问题描述】:

假设我的数据如下所示

   student article.bought
1        A            pen
2        B         pencil
3        V           book
4        A            pen
5        A      inkbottle
6        B            pen
7        B         pencil
8        B         pencil
9        V           book
10       Z         marker
11       A      inkbottle
12       V           book
13       V            pen
14       V           book

我需要像这样的不同列中可能出现的独特文章

   student article.bought Occurences
1        A            pen          1
2        B         pencil          1
3        V           book          1
4        A            pen          1   # as A is taking a pen again
5        A      inkbottle          2   # 'A' changed from pen to ink bottle
6        B            pen          2
7        B         pencil          3   # though B took pencil before, this is different as he took a pen in between
8        B         pencil          3
9        V           book          1
10       Z         marker          1
11       A      inkbottle          2
12       V           book          1
13       V            pen          2
14       V           book          3

【问题讨论】:

  • 感谢您的快速回复。文章应该被唯一标记。如果学生接受一篇文章,它应该标记为 1,下一篇文章应该标记为 2,依此类推。如果文章立即重复编号应保持不变,如果在中间取了一些文章后重复该文章,则该文章应视为不同的文章
  • 所以你想在一个学生从一篇文章到另一篇文章的过渡时增加一个数值?原始排序顺序有什么意义吗?当学生之前使用的产品发生转换时(但不是紧接在之前......例如pen-book-pen),这个数值应该做什么?
  • 是的,当一名学生从一篇文章过渡到另一篇文章时,想要增加一个数值。
  • 11.墨水瓶不应该是2个吗?自上次出现 A 以来,它没有改变。
  • 是的,排序顺序很重要。当转换到学生以前使用的产品时,数值也应该增加。对于一个学生,pen 可以是 1,对于另一个学生,它可以是 3..

标签: sql r excel sas worksheet-function


【解决方案1】:

在 R 中,我们可以通过查找每个后续值的差异 diff 来发现学生选择的变化。当我们获取该逻辑索引的累积总和 cumsum 时,我们会得到出现的运行计数。

在第二行中,我们将因子变量 article.bought 强制转换为数值,并使用 ave 从第一行运行函数,以按学生对函数 f 进行分组。

f <- function(x) cumsum(c(F, diff(x) != 0)) + 1
df$Occurences <- with(df, ave(as.numeric(article.bought), student, FUN=f))
df
#    student article.bought Occurences
# 1        A            pen          1
# 2        B         pencil          1
# 3        V           book          1
# 4        A            pen          1
# 5        A      inkbottle          2
# 6        B            pen          2
# 7        B         pencil          3
# 8        B         pencil          3
# 9        V           book          1
# 10       Z         marker          1
# 11       A      inkbottle          2
# 12       V           book          1
# 13       V            pen          2
# 14       V           book          3

【讨论】:

  • 是的!它的工作..非常感谢你..非常有帮助
【解决方案2】:
  1. 创建附加列 [原始排序顺序] 并从 1 枚举 到...
  2. 按学生/原始排序顺序对表进行排序
  3. 在 D2 中输入=IF(A2=A1,IF(B2=B1,D1,D1+1),1) 并向下复制
  4. 将 D 列转换为值(复制、粘贴为...值)
  5. 恢复原始排序顺序

如果这不是一次性的,请使用相同的策略来创建 VBA 脚本

【讨论】:

  • 很好。非常感谢。
【解决方案3】:

与 SAS 合影:

data try00;
length student article $20;
infile datalines dlm=' ';
input student $ article $;
datalines;
A pen
B pencil 
V book 
A pen 
A inkbottle 
B pen 
B pencil 
B pencil 
V book 
Z marker 
A inkbottle
V book 
V pen 
V book
;

data try01;
set try00;
pos=_n_;
run;

proc sort data=try01 out=try02; by student pos article; run;

proc sort data=try02 out=stud(keep=student) nodupkey; by student; run;

data shell;
length occurrence 8.;
set try02;
if _n_>0 then delete;
run;

%macro loopstudent();

data _null_; set stud end=eof; if eof then call symput("nstu",_n_); run;


%do i=1 %to &nstu;
data _null_; set stud; if _n_=&i then call symput("stud&i",student); run;

data thisstu;
set try02;
where student="&&stud&i";
dummyart=lag(article);
retain occurrence 0;
if dummyart ne article then occurrence=occurrence+1;
else occurrence=occurrence;
drop dummyart;
run;

proc append base=shell data=thisstu; run;

%end;

proc sort data=shell out=final; by pos; run;

%mend loopstudent; %loopstudent();

数据集“final”有结果。

【讨论】:

    猜你喜欢
    • 2018-05-14
    • 2018-09-22
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    相关资源
    最近更新 更多