4
\$\begingroup\$

I have a data frame called data with lots of columns, and Country is one of them.

I created a new logical column called train, which is true when the row belongs to the train set and false otherwise. The data frame is already sampled.

Now I want to be sure that there are no countries in both train and test sets with this code:

nrow( data[ data[ data$train, ]$Country %in% data[! data$train, ]$Country, ] ) == 0

which evaluates to true, so I guess I sampled correctly. But that line makes my eyes bleed.

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Oct 15, 2013 at 16:48
\$\endgroup\$
1
  • 1
    \$\begingroup\$ As data is also the name of a function in the core utils package, it is not good practice to use it for the name of an object. \$\endgroup\$ Commented Nov 11, 2013 at 17:15

1 Answer 1

3
\$\begingroup\$

I would do:

with(data, length(intersect(Country[train], Country[!train])) == 0)
answered Oct 16, 2013 at 0:30
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.