R 基于字符串条件创建子集
拥有这样的数据帧:R 基于字符串条件创建子集,r,R,拥有这样的数据帧: df_in <- data.frame(x = c('x1','x2','x3','x4'), col1 = c('http://youtube.com/something','NA','https://www.yahooexample.com','https://www.yahooexample2.com'), col2 = c('https://google.com', 'http:
df_in <- data.frame(x = c('x1','x2','x3','x4'),
col1 = c('http://youtube.com/something','NA','https://www.yahooexample.com','https://www.yahooexample2.com'),
col2 = c('https://google.com', 'http://www.bbcnews2.com?id=321','NA','https://google.com/text'),
col3 = c('http://www.bbcnews.com?id=321', 'http://google.com?id=1234','NA','https://bbcnews.com/search'),
col4 = c('NA', 'https://www.youtube/com','NA', 'www.youtube.com/searcht'))
我想创建一个特定子集条件的数据帧。例如,我只想保留一个包含“谷歌”、“youtube”和“bbc”的标签。
预期产出示例:
df_out <- data.frame(x = c('x1','x2','x4'),
col1new = c('http://youtube.com/something', 'http://www.bbcnews2.com?id=321', 'https://google.com/text'),
col2new = c('https://google.com', 'http://google.com?id=1234', 'https://bbcnews.com/search'),
col3new = c('http://www.bbcnews.com?id=321', 'https://www.youtube/com', 'www.youtube.com/searcht'))
我们可以使用
grep
创建逻辑条件,根据http://
i1 <- Reduce('|', lapply(df_in[-1], grepl, pattern= "https?://(google|youtube|bbc)"))
和cbind
以及第一列的子集
cbind(df_in[i1, 1, drop = FALSE], tmp)
# x col1new col2new col3new
#1 x1 http://youtube.com/something https://google.com http://www.bbcnews.com?id=321
#2 x2 http://www.bbcnews2.com?id=321 http://google.com?id=1234 https://www.youtube/com
#4 x4 https://google.com/text https://bbcnews.com/search www.youtube.com/searcht
你试过什么?当你的搜索小组发现你的情况时会发生什么?例如
bbc
中的youtube
<代码>https://www.youtube.com/results?search_query=bbc您可能需要i1的输出是什么:df\u in
i1 <- Reduce('|', lapply(df_in[-1], grepl, pattern= "https?://(google|youtube|bbc)"))
tmp <- t(apply(df_in[i1,-1], 1, function(x) x[grepl("(google|youtube|bbc)", x)]))
colnames(tmp) <- paste0('col', seq_len(ncol(tmp)), "new")
cbind(df_in[i1, 1, drop = FALSE], tmp)
# x col1new col2new col3new
#1 x1 http://youtube.com/something https://google.com http://www.bbcnews.com?id=321
#2 x2 http://www.bbcnews2.com?id=321 http://google.com?id=1234 https://www.youtube/com
#4 x4 https://google.com/text https://bbcnews.com/search www.youtube.com/searcht