Vegas588
Vegas588

Reputation: 329

Split Data Frame R

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

Upvotes: 0

Views: 139

Answers (1)

Sonny
Sonny

Reputation: 3183

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)

Upvotes: 1

Related Questions