הסרת שורות צמודות זהות

11 views
Skip to first unread message

Avner Kantor

unread,
Apr 24, 2018, 11:07:48 AM4/24/18
to israel-r-user-group
אני רוצה להסיר שורות זהות בתנאי שהן צמודות זו לזו. איך עושים זאת?
למשל
x="a"
y=c(1, 2, 2, 2, 3, 2, 2, 3)
df<-data.frame(x,y)
x y 1 a 1 2 a 2 3 a 2 4 a 2 5 a 3 6 a 2 7 a 2 8 a 3

אני רוצה לקבל
  x y
1 a 1
2 a 2
3 a 3
4 a 2
5 a 3

amit gal

unread,
Apr 24, 2018, 11:17:56 AM4/24/18
to israel-r-...@googlegroups.com
library(tidyverse)

ldf = df %>% mutate_all(funs(lag))

result = df[- which((rowSums(df==ldf)==ncol(df))),]

הסבר: באופן כללי אתה לא רוצה ששורה תהיה דומה לקודמתה.
השורה הראשונה בקוד מייצרת הסטה של כל שורה בדאטה שלך אחד למטה. בשורה השניה אני משווה בין הדאטה המקורי לדאטה המוסט. אני סוכם את השורות. אם סכום ההשוואה הזו הוא כמספר הטורים זה אומר שכל השורה שווה לקודמתה. על פני כל השורות שמקיימות את התנאי הזה - אני זורק אותן מהדאטה המקורי. לתפארת מדינת ישראל.

מקווה שזה עוזר.
עמית


--
You received this message because you are subscribed to the Google Groups "Israel R User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-group+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Dorman

unread,
Apr 24, 2018, 11:18:17 AM4/24/18
to israel-r-...@googlegroups.com

2018-04-24 18:07 GMT+03:00 Avner Kantor <avner...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages