1

Good night, i have a file like this

City Lat   Long  Mals 
Bog 1m2sS 05M34W 2000 
Bog 1m2sS 05M34W 2000 
Bog 1m2sS 05M34W 3500 
Bog 6m3sS 10M34W 3400

I want to get a file like the following:

City Lat   Long  Mals
Bog 1m2sS 05M34W 2000
Bog 1m2sS 05M34W 3500
Bog 6m3sS 10M34W 3400

Thanks for your help

Johanna Ramirez
  • 161
  • 1
  • 9

2 Answers2

2

It would help to have more information, like an example of your data.

You can use the janitor package to identify duplicate responses:

library(janitor)
library(dplyr)

# Get all duplicates
df %>% get_dupes()

# Get duplicates for a specified variable
df %>% get_dupes(var_name)
Matt
  • 7,255
  • 2
  • 12
  • 34
2

We can use unique from base R

unique(df1)
#  City   Lat   Long Mals
#1  Bog 1m2sS 05M34W 2000
#3  Bog 1m2sS 05M34W 3500
#4  Bog 6m3sS 10M34W 3400

data

df1 <- structure(list(City = c("Bog", "Bog", "Bog", "Bog"), Lat = c("1m2sS", 
"1m2sS", "1m2sS", "6m3sS"), Long = c("05M34W", "05M34W", "05M34W", 
"10M34W"), Mals = c(2000L, 2000L, 3500L, 3400L)),
class = "data.frame", row.names = c(NA, 
-4L))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Hi akrun i was trying this but it gave error tablef<-unique(table, order(CITY, DEPTO_FARM, LAT, LONG)) – Johanna Ramirez Apr 23 '20 at 22:22
  • @JohannaRamirez that won't work. YOu may need `unique(table[with(table, order(CITY, DEPTO_FARM, LAT, LONG)),])` – akrun Apr 23 '20 at 22:24
  • I had tried too, but it didn't work, however unique (data) is working for now...thanks a lot!!!! – Johanna Ramirez Apr 23 '20 at 22:37
  • @JohannaRamirez do you have a data.frame or tibble or data.table – akrun Apr 23 '20 at 22:37
  • I have a data.frame – Johanna Ramirez Apr 23 '20 at 22:43
  • @JohannaRamirez ii am not sure about the structure of your data. THe code is working with the data in my post `unique(df1[with(df1, order(City, Lat, Long, Mals)), ])` – akrun Apr 23 '20 at 22:45
  • this is the error that appears, but previously you have read the variables, i don't understand repli <- unique(table[with(table, order(CITY, DEPTO_FARM, LAT, LONG)),]) Error in order(CITY, DEPTO_FARM, LAT, LONG) : oggetto "CITY" non trovato – Johanna Ramirez Apr 24 '20 at 08:04
  • @JohannaRamirez can you please translate that error in english. Is it because you have a different column name? – akrun Apr 24 '20 at 17:57
  • sorry, "CITY" object not found. No, the names are correct, but I can't understand what's going on... – Johanna Ramirez Apr 24 '20 at 18:03
  • @JohannaRamirez if you look at my data, the column name is "City". instead of "CITY' and it iis working fine. Here, in the order, it is ordering first by 'CITY', 'DEPTO', 'FARM', LAT, LONG assuming these columns are found in the data, and it returns an index that is used as row index to order the rows – akrun Apr 24 '20 at 18:05
  • @JohannaRamirez what is the output of `with(table, CITY)` – akrun Apr 24 '20 at 18:06
  • the result are the values of City Var, i.e. "Bogota" "Cali" "Villavc"...etc – Johanna Ramirez Apr 24 '20 at 18:12
  • @JohannaRamirez, then it should have worked for you. Can you update your post with the `dput` of data i.e. `dput(droplevels(head(table)))`. It gives the structure of your data so that I can test whether there is any issue in that dataset – akrun Apr 24 '20 at 18:14
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/212438/discussion-between-johanna-ramirez-and-akrun). – Johanna Ramirez Apr 24 '20 at 18:18