简体   繁体   中英

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM