longer object length and shorter object length?

Hello community, I am trying to filter my data using its index, but I keep getting this error.

longer object length is not a multiple of shorter object length

I don't understand why this is happening, because I didn't do any mathematic operations. I want to exclude certain rows, but it doesn't work. Can someone help me with this?
Thanks in advance!

G$Index <- as.character(G$Index)
narr <- G %>%
  filter(Genre == "narrative", Index != c("12","21","20","16","23","11","13")) %>%
  select(Index, File.name,Word.count.tier.1, Length.of.recording) %>%
  arrange(desc( factor(Word.count.tier.1)))

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

Agree with Nir, a reprex will help but the following is worth a look.
To compare a vector use !var %in% vec instead of !=

Thank you Nir! :smile: Here is my reprex example: (I hope it works.)

df <- data.frame(
  stringsAsFactors = FALSE,
             Index = c(27L, 22L, 17L, 25L, 12L, 3L),
          Filename = c("f99Ogoelong","f99Dliit",
                       "f99Akur","f99Dreep","f00Jfuan","c00ANyouth3"),
             Genre = c("Traditional narrative (with singing)","Traditional narrative","Traditional narrative",
                       "Traditional narrative","Traditional narrative",
                       "Conversation"),
          duration = c("0:11:24","0:08:57","0:05:26",
                       "0:07:23","0:10:04","0:06:29"),
         Wordcount = c(1840L, 1413L, 1302L, 1226L, 1156L, 1153L)
)

narr <- df %>%
    filter(Genre == "Traditional narrative") %>%
    filter(Index != c(12, 21, 20,16,23,11,13)) %>%
    arrange(desc( factor(Wordcount)))

Still, I got the same error:

longer object length is not a multiple of shorter object length

Hi!
Thank you for your suggestion!

I tried !%in% as here in the example:

library(dplyr)
df <- data.frame(
  stringsAsFactors = FALSE,
             Index = c(27L, 22L, 17L, 25L, 12L, 3L),
          Filename = c("f99Ogoelong","f99Dliit",
                       "f99Akur","f99Dreep","f00Jfuan","c00ANyouth3"),
             Genre = c("Traditional narrative (with singing)","Traditional narrative","Traditional narrative",
                       "Traditional narrative","Traditional narrative",
                       "Conversation"),
          duration = c("0:11:24","0:08:57","0:05:26",
                       "0:07:23","0:10:04","0:06:29"),
         Wordcount = c(1840L, 1413L, 1302L, 1226L, 1156L, 1153L)
)

G$Index <- as.character(G$Index)
narr <- G %>%
  filter(Genre == "Traditional narrative") %>%
  filter(Index !%in% c("12","21","20","16","23","11","13")) %>%
  arrange(desc( factor(Wordcount)))

It failed either, the error is

Error: unexpected '!' in:
"  filter(Genre == "Traditional narrative") %>%
  filter(Index !"

Is that due to the library? :face_with_monocle:

Nope. It's because !var %in% vec in not the same as Index !%in% c(12...

Note carefully the position of the !

I just tried it on your reprex - works fine.

Thank you! The problem has been solved. :+1:t2:

But I still don't understand why it didn't work with != or !%in%, though I tried to convert the index into numeric and character forms (maybe the form has nothing to do with the error?)

  1. Because !%in%, insn't a thing
    • try just 2 %in% 1:3 then ! 2 %in% 1:3 and read ?%in%`
  2. This is what the error message longer object length is not a multiple of shorter object length was trying to say in R speak
    • What would you expect the answer to 1 == 1,2,3 to be?
    • 1 is clearly not equal to 1,2,3, because they are different lengths

Oh sorry for my mistake and stupid question.
Thanks a lot for the explanation. I got it now.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.