0

I'm clearly going about this incorrectly, so looking for some advice (newbie R programmer here...) Need to split up a data frame of the AFINN word list into two new vectors (one for positive words and one for negative words). I used subset, but have a few lines here. What's a better way to combine these lines into one line?

# read the "AFINN" dataset and assign it into a variable called "AFINN"
AFINN <- read.delim("AFINN.txt", header=FALSE)
AFINN
# change column names to "Word" and "Score"
colnames(AFINN) <- c("Word","Score")
#split the AFINN data frame up into positive and negative word vectors
posAFINN <- subset(AFINN, Score >= 0)
posAFINN <- posAFINN[,-2]
posAFINN

negAFINN <- subset(AFINN, Score <= 0)
negAFINN <- negAFINN[,-2]
negAFINN
2
  • 3
    Can it be >= and just <? If so split(AFINN$Word, AFINN$Score >= 0) will do it. Or lapply(split(AFINN, AFINN$Score >= 0), '[', -2, drop = FALSE) Commented Mar 22, 2019 at 16:50
  • split(AFINN$Word, sign(AFINN$Score)) is an alternative putting zero in a third vector. Commented Mar 22, 2019 at 16:53

1 Answer 1

1

Base R:

posAFINN <- AFINN$Word[AFINN$Score > 0]
negAFINN <- AFINN$Word[AFINN$Score < 0]

Dplyr:

library(dplyr)
posAFINN <- AFINN %>%
    filter(Score > 0) %>%
    pull(Word)

negAFINN <- AFINN %>%
    filter(Score < 0) %>%
    pull(Word)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.