Common elements

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Common elements

Silvano-7
Hi,

I have a dataset with ten columns, but I need extract only lines that has
common elements.
The columns are:

      Animal Mother
 [1,]   1143    430
 [2,]   1144    134
 [3,]   1146      3
 [4,]   1147    151
 [5,]   1150    230
 [6,]   1156    290
 [7,]   1157    227
 [8,]   1159    757
 [9,]   1160      3
[10,]   1161    236
[11,]   1162    231
[12,]   1164    132
[13,]   1165    420
[14,]   1168    290
[15,]   1169    229
[16,]   1172    425
[17,]   1173    134
[18,]   1174    234
[19,]   1175    233
[20,]   1178    239
[21,]   1179    757
[22,]   1180    236
[23,]   1185    420
[24,]   1186    389
[25,]   1190    425
[26,]   1192    235

How can I do this?

Thanks.

Prof. Dr. Silvano Cesar da Costa
Universidade Estadual de Londrina
Centro de Ciências Exatas
Departamento de Estatística

Fone: (43) 3371-4346

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Common elements

Jeff Newmiller
This request is quite unclear. Can you make a reproducible example [1][2][3] and provide data that represents the expected result corresponding to the input data? The dput function is much much better than tabular form for this purpose.

For one thing, you mention ten columns, but they are not shown?

[1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

[2] http://adv-r.had.co.nz/Reproducibility.html

[3] https://cran.r-project.org/web/packages/reprex/index.html (read the vignette)

On March 8, 2019 9:51:20 AM PST, Silvano Cesar da Costa <[hidden email]> wrote:

>Hi,
>
>I have a dataset with ten columns, but I need extract only lines that
>has
>common elements.
>The columns are:
>
>      Animal Mother
> [1,]   1143    430
> [2,]   1144    134
> [3,]   1146      3
> [4,]   1147    151
> [5,]   1150    230
> [6,]   1156    290
> [7,]   1157    227
> [8,]   1159    757
> [9,]   1160      3
>[10,]   1161    236
>[11,]   1162    231
>[12,]   1164    132
>[13,]   1165    420
>[14,]   1168    290
>[15,]   1169    229
>[16,]   1172    425
>[17,]   1173    134
>[18,]   1174    234
>[19,]   1175    233
>[20,]   1178    239
>[21,]   1179    757
>[22,]   1180    236
>[23,]   1185    420
>[24,]   1186    389
>[25,]   1190    425
>[26,]   1192    235
>
>How can I do this?
>
>Thanks.
>
>Prof. Dr. Silvano Cesar da Costa
>Universidade Estadual de Londrina
>Centro de Ciências Exatas
>Departamento de Estatística
>
>Fone: (43) 3371-4346
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Common elements

Doran, Harold
In reply to this post by Silvano-7
Do you mean like this?

tmp <- data.frame(v1 = c(1,2,1,4,2), v2 = c(10, 11, 10, 14, 11))
vals <- paste(tmp$v1, tmp$v2, sep ='')
tmp[which(vals %in% vals[duplicated(vals)]),]



On 3/8/19, 12:51 PM, "Silvano Cesar da Costa" <[hidden email]> wrote:

>Hi,
>
>I have a dataset with ten columns, but I need extract only lines that has
>common elements.
>The columns are:
>
>      Animal Mother
> [1,]   1143    430
> [2,]   1144    134
> [3,]   1146      3
> [4,]   1147    151
> [5,]   1150    230
> [6,]   1156    290
> [7,]   1157    227
> [8,]   1159    757
> [9,]   1160      3
>[10,]   1161    236
>[11,]   1162    231
>[12,]   1164    132
>[13,]   1165    420
>[14,]   1168    290
>[15,]   1169    229
>[16,]   1172    425
>[17,]   1173    134
>[18,]   1174    234
>[19,]   1175    233
>[20,]   1178    239
>[21,]   1179    757
>[22,]   1180    236
>[23,]   1185    420
>[24,]   1186    389
>[25,]   1190    425
>[26,]   1192    235
>
>How can I do this?
>
>Thanks.
>
>Prof. Dr. Silvano Cesar da Costa
>Universidade Estadual de Londrina
>Centro de Ciências Exatas
>Departamento de Estatística
>
>Fone: (43) 3371-4346
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Common elements

Rui Barradas
In reply to this post by Silvano-7
Hello,

If I understand correctly, here are two different ways of doing what you
want.

1) base R.

i <- duplicated(df1[[2]])
j <- duplicated(df1[[2]], fromLast = TRUE)
res <- df1[i | j, ]

res[order(res[[2]]), ]    # not strictly needed


2) with package dplyr. If you do not want to order by Mother,
delete the last %>%, at the end of the line and the next line,
arrange(Mother).

library(dplyr)

df1 %>% group_by(Mother) %>% filter(n() > 1) %>%
   arrange(Mother)


#------------ dataset ------------------
# Note that your data seems to be a matrix,
# read.table creates data.frames

df1 <- read.table(text = "
       Animal Mother
[1,]   1143    430
[2,]   1144    134
[3,]   1146      3
[4,]   1147    151
[5,]   1150    230
[6,]   1156    290
[7,]   1157    227
[8,]   1159    757
[9,]   1160      3
[10,]   1161    236
[11,]   1162    231
[12,]   1164    132
[13,]   1165    420
[14,]   1168    290
[15,]   1169    229
[16,]   1172    425
[17,]   1173    134
[18,]   1174    234
[19,]   1175    233
[20,]   1178    239
[21,]   1179    757
[22,]   1180    236
[23,]   1185    420
[24,]   1186    389
[25,]   1190    425
[26,]   1192    235
", header = TRUE)
row.names(df1) <- NULL



Hope this helps,

Rui Barradas


Às 17:51 de 08/03/2019, Silvano Cesar da Costa escreveu:

> Hi,
>
> I have a dataset with ten columns, but I need extract only lines that has
> common elements.
> The columns are:
>
>        Animal Mother
>   [1,]   1143    430
>   [2,]   1144    134
>   [3,]   1146      3
>   [4,]   1147    151
>   [5,]   1150    230
>   [6,]   1156    290
>   [7,]   1157    227
>   [8,]   1159    757
>   [9,]   1160      3
> [10,]   1161    236
> [11,]   1162    231
> [12,]   1164    132
> [13,]   1165    420
> [14,]   1168    290
> [15,]   1169    229
> [16,]   1172    425
> [17,]   1173    134
> [18,]   1174    234
> [19,]   1175    233
> [20,]   1178    239
> [21,]   1179    757
> [22,]   1180    236
> [23,]   1185    420
> [24,]   1186    389
> [25,]   1190    425
> [26,]   1192    235
>
> How can I do this?
>
> Thanks.
>
> Prof. Dr. Silvano Cesar da Costa
> Universidade Estadual de Londrina
> Centro de Ciências Exatas
> Departamento de Estatística
>
> Fone: (43) 3371-4346
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.