按行选择第一个不缺少的元素 [英] Pick out first non missing element by row
本文介绍了按行选择第一个不缺少的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我想要执行以下操作:
Suppose I want to do the following:
1)应用一些函数,它们用空格逐行替换不需要的字符;
2)然后它选择(从原始数据中)逐行第一个非空元素(根据上面的转换)。
1) Apply some function, which replaces not needed characters by empty spaces row by row; 2) Then it picks out (out of the original data) row by row first non empty element (according to the above transformation).
这里是我现在的:
library(data.table)
data<-data.table(x=c("25&&&35&&1","&&&&","&&&&"),
y=c("&&&&&","1","&&&&2"),
z=c("&&&&&","","1"))
function_select<-function(x){
x[gsub("&","",x)!=""][1]
}
data[,function_select(unlist(.SD)),.SDcols=c("x","y"),by=1:nrow(data)]
nrow V1
1: 1 25&&&35&&1
2: 2 1
3: 3 &&&&2
我有大约7000万行,问题:
1)使用data.table有更快的解决方案;
2)我可以使用parLapply的想法加快计算速度;
I have roughly 70 millions of rows, I have two questions: 1) Is there a faster solution in using data.table; 2) Can I speed up my calculations using ideas from parLapply;
推荐答案
鼠疫。这里我可能会:
data[, lapply(.SD, sub, pattern = "^&*$", replacement = ""), .SDcols = x:y][
, as.matrix(.SD)[.SD != ""]]
#[1] "25&&&35&&1" "1" "&&&&2"
这篇关于按行选择第一个不缺少的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文