0
$\begingroup$

Situation: I'm trying to program the following in r.

Task: I am trying to select for words that appear as nouns in my dataset more than they do as adjectives, verbs, or adverbs etc. I have all these counts and below is an example of one instance of what I am trying to do. Imagine the information below is in a dataframe. I do not want to select for this lemma (ability), because it appears most times as a VERB; i.e., its appearance as a noun is not greater than VERB or ADJ:

id <- (c(4, 4, 4))
lemma <- (c("ability", "ability", "ability"))
count_lemma+pos <- (21, 66, 89332)
pos <- ("ADJ", "NOUN", "VERB) 

Action: I tried to start programming the fail below to get to the following logic:

  1. group the data by id
  2. for every row i id, check if pos == "NOUN"
  3. If not, then delete the row in id
  4. check id for max value
  5. return pos
  6. pos != "NOUN", then delete id

#This is my failed attempt at the first step in r:

noun_count_all <- ddply(noun_count, .(lemma), function(noun_count) {
  filter1 <- filter(noun_count, pos=="NOUN")
  #filter2 <-
  return(filter1)
} )

Result: Not getting anywhere. If I've written this question incorrectly, sorry about that. Not a programmer or data scientist, I'm just trying to use R to do this thing I can't do in excel.

$\endgroup$
5
  • $\begingroup$ I think I understand what you mean with almost all of your steps except 4 and 5. What do you want to do with the id column, just see the max value or filter on rows where the id is equal to the maximum id within the group? And for 5, you want to return all values for pos column or filtered on something? $\endgroup$
    – Oxbowerce
    Aug 3, 2021 at 14:12
  • $\begingroup$ My apologies @Oxbowerce that steps 4 + 5 were not clear. In step 4, I really just want to check whether the row in the group (which I can group by id or lemma) with the highest value (e.g., out of 1 4 6 it would be 6) is a noun. This is linked to step 5. Step 5 is not so necessary, but was more to manually chceck inside the loop if I wanted to. Then step 6 is saying: please delete all of the rows containing highest values, in the group, which are not nouns. Does that make more sense? $\endgroup$
    – n.baes
    Aug 3, 2021 at 14:25
  • $\begingroup$ p.s. I am trying to filter for all "NOUN" in pos which are the highest value in each group by id; if note then delete. $\endgroup$
    – n.baes
    Aug 3, 2021 at 14:34
  • $\begingroup$ I think so, but just to be sure can you provide the expected output given the inputs you provided? Also, how can you remove rows where pos != "NOUN" if you already filtered out those rows in step 2/3? $\endgroup$
    – Oxbowerce
    Aug 3, 2021 at 14:35
  • $\begingroup$ Yeah, so given the inputs above the lemma "ability or you could think of it as the id 4 would be removed from the final data frame because it is not the highest value in the distribution of its set. Step 2/3 was a convoluted way of getting rid of any id sets which did not have any nouns in them. Very inefficient in hindsight... $\endgroup$
    – n.baes
    Aug 3, 2021 at 14:58

1 Answer 1

1
$\begingroup$

Using dplyr, the following code selects only the rows where the pos column has the value "NOUN" and where the count_lemma+pos is the highest within the group.

library(dplyr)

df %>%
    # group by id
    groupby(id) %>%
    # filter on rows where pos == "NOUN" and count_lemma_pos is the max value within the group
    filter(pos == "NOUN" & count_lemma_pos == max(count_lemma_pos))
$\endgroup$
3
  • $\begingroup$ That is it! It is ridiculous how much I complicated things compared to how simple the solution is. Thank you so much @Oxbowerce. I hope this is helpful for other people too. $\endgroup$
    – n.baes
    Aug 3, 2021 at 14:53
  • 1
    $\begingroup$ No worries, if it solved your problem make sure to mark this as the answer so other know the solution as well. $\endgroup$
    – Oxbowerce
    Aug 3, 2021 at 14:57
  • $\begingroup$ Thank you @Oxbowerce, I really appreciate your help and hope I learn to be more efficient in the future. $\endgroup$
    – n.baes
    Aug 3, 2021 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.