I've a dataframe named df
. I want to check, for each document, if the xcoordinate and ycoordinate are the same.
doc <- c("doc1", "doc2", "doc3")
xcor <- c(3,4,5,3,4,5,3,4,4)
ycor <- c(2,6,8,2,6,8,2,6,8)
df <- data.frame(doc,xcor,ycor)
df
doc xcor ycor
doc1 3 2
doc2 4 6
doc3 5 8
doc1 3 2
doc2 4 6
doc3 5 8
doc1 3 2
doc2 4 6
doc3 4 8
I'm now struggling with all the apply functions, but this doesn't result in the desire outcome.
Which is
doc1 TRUE
doc2 TRUE
doc3 FALSE
I don't think you need an apply
function for this. We can count the number of repeat docs after eliminating duplicate rows:
table(df[!duplicated(df),]$doc) == 1
doc1 doc2 doc3
TRUE TRUE FALSE
Or even shorter (@DavidArenburg):
table(unique(df)$doc) == 1
Well, you can still achieve it with lapply
:
unlist(lapply(lapply(lapply(split(df, doc), unique), nrow), `==`, 1))
Looks kind of messy, but it works.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.