我想在数据框中创建一个新列,它是TRUE或FALSE,具体取决于一个术语是否出现在两个指定的列中。 这是一些示例数据:
AB <- c('CHINAS PARTY CONGRESS','JAPAN-US RELATIONS','JAPAN TRIES TO')
TI <- c('AMERICAN FOREIGN POLICY', 'CHINESE ATTEMPTS TO', 'BRITAIN HAS TEA')
AU <- c('AUTHOR 1', 'AUTHOR 2','AUTHOR 3')
M <- data.frame(AB,TI,AU)
我可以为一个列或另一个列执行此操作,但我无法弄清楚如何为这两个列执行此操作。换句话说,我不知道如何将这两条不会互相覆盖的行组合起来。
M$China <- mapply(grepl, "CHINA|CHINESE|SINO", x=M$AB)
M$China <- mapply(grepl, "CHINA|CHINESE|SINO", x=M$TI)
重要的是我指定列,我不能选择整个data.frame.I已经找到了其他类似的问题,但似乎没有一个适用于我的情况,我无法适应任何现有的例子。这对我来说是有意义的:
M$China <- mapply(grepl, "CHINA|CHINESE|SINO", x=(M$AB|M$TI)
答案 0 :(得分:5)
使用:
M$China <- !!rowSums(sapply(M[1:2], grepl, pattern = "CHINA|CHINESE|SINO"))
给出:
> M AB TI AU China 1 CHINAS PARTY CONGRESS AMERICAN FOREIGN POLICY AUTHOR 1 TRUE 2 JAPAN-US RELATIONS CHINESE ATTEMPTS TO AUTHOR 2 TRUE 3 JAPAN TRIES TO BRITAIN HAS TEA AUTHOR 3 FALSE
这是做什么的:
sapply(M[1:2], grepl, pattern = "CHINA|CHINESE|SINO")
遍历两个AB
和TI
列,并查看模式的某个部分("CHINA|CHINESE|SINO"
)是否存在。 sapply
- 调用返回TRUE
/ FALSE
个值的矩阵:
AB TI
[1,] TRUE FALSE
[2,] FALSE TRUE
[3,] FALSE FALSE
使用rowSums
,您可以查看每行有多少TRUE
个值。
!!
前面添加rowSums
,您可以将rowSums
中的所有值转换为高于零的TRUE
,将所有内容转换为FALSE
。 答案 1 :(得分:1)
如果我们需要折叠为单个向量,请使用Map
循环遍历列,应用pattern
获取list
logical
向量,然后Reduce
使用logical
|
向量转换为M$China <- Reduce(`|`, Map(grepl, "CHINA|CHINESE|SINO", M))
M
# AB TI AU China
#1 CHINAS PARTY CONGRESS AMERICAN FOREIGN POLICY AUTHOR 1 TRUE
#2 JAPAN-US RELATIONS CHINESE ATTEMPTS TO AUTHOR 2 TRUE
#3 JAPAN TRIES TO BRITAIN HAS TEA AUTHOR 3 FALSE
向量
tidyverse
或在library(tidyverse)
M %>%
mutate_all(funs(str_detect(., "CHINA|CHINESE|SINO"))) %>%
reduce(`|`) %>%
mutate(M, China = .)
import math
prime_number = 4 # Because 2 and 3 are already prime numbers
k = 3 # It is the 3rd try after 2 and 3 prime numbers
milestone = 10001
while k <= milestone:
divisible = 0
for i in range(2, int(math.sqrt(prime_number)) + 1):
remainder = prime_number % i
if remainder == 0: #Check if the number is evenly divisible (not prime) by i
divisible += 1
if divisible == 0:
k += 1
prime_number += 1
print(prime_number-1)