r - Create a dummy to indicating presence of string fragment in any of multiple variables
问题描述
df <- data.frame (address.1.line = c("apartment 5", "25 spring street", "nice house"), address.2.line = c("london", "new york", "apartment 2"), address.3.line = c("", "", "paris"))
I'm trying to make a function that returns a new column in a data frame. The column should be a dummy variable attached to the original data frame indicating whether any of 3 address-line variables contain a string (or selection of strings).
E.g., in the example above, I want df to have a new variable called "Apartment_dummy" indicating the presence of the string fragment "apartment" in any of the three address lines---so it will take 1 in rows 1 and 3, and zero in row 0. The function needs to take 2 arguments, therefore: the name of the new dummy variable to be created, and the corresponding string fragment that needs to be detected in the address variables.
I'd tried the following. It will return a dummy, but won't give the new variable the right name. Also, I feel like there must be a way to do it in a single step. Any ideas? Many thanks!
library(tidyverse)
premises_dummy <- function(varname = NULL, strings = NULL) {
df %<>% mutate_at(.funs = funs(flagA = str_detect(., strings)), .vars = vars(ends_with(".line"))) %>%
mutate(varname = ifelse(rowSums(select(., contains("flagA"))) > 0, 1, 0))
return(df)
}
df <- premises_dummy(varname = 'Apartment_dummy', strings = 'apartment')
解决方案
A quick data.table
solution to it:
library(data.table)
dt <- data.table(df)
search_string <- "apartment"
dt[like(address.1.line, search_string)|
like(address.2.line, search_string)|
like(address.3.line, search_string), paste0(search_string,".Dummy") := 1]
dt[is.na(get(paste0(search_string,".Dummy"))), paste0(search_string,".Dummy") := 0]
推荐阅读
- java - 中断异常捕获中的方法调用是否会完成 run() 方法并使线程完成
- python - 重叠轮廓
- json - 如果存在,如何从 JSON 中获取特定值
- r - 使用R、dplyr和rowwise时如何定义一个变量来记录处理的行数?
- php - 紧凑():未定义的变量:运算符 - Laravel
- selenium - Selenium Headless Firefox - 非常慢
- python-3.x - Skimage RAG 合并灰度图像
- node.js - 卡在 Node.js 中的 oracle Ref 光标
- vue.js - 条目模式“'./src/components/*.vue'”不匹配任何文件/构建 Web 组件 VUEJS
- sql-server - 比较两个不同列的时间并创建显示时间差异的第三列