分割数据框R

时间:2019-03-22 16:46:55

标签: r

我显然没有正确地处理此问题,因此需要寻找一些建议(这里是新手R程序员...)。需要将AFINN单词列表的数据帧拆分为两个新的向量(一个用于肯定词,另一个用于否定词)。我使用了子集,但是这里有几行。将这些行合并为一行的更好的方法是什么?

# 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

1 个答案:

答案 0 :(得分:0)

基本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)