我想删除所有最后一个值为零的行以及所有最终值为零的列。
这是我的数据集的虚拟(可重现)示例:
library(dplyr)
x = c("apples" ,1,0,1,2)
y = c("bananas",0,0,0,0)
z = c("apples" ,2,0,4,6)
t = c("rowsum" ,3,0,5,8)
my_table = rbind(x,y,z,t)
colnames(my_table) = c("product","day1","day2","day3","colsum")
my_table = as.tbl(as.data.frame(my_table)) %>%
mutate(day1 = as.integer(as.character(day1)),
day2 = as.integer(as.character(day2)),
day3 = as.integer(as.character(day3)),
colsum = as.integer(as.character(colsum)))
虚拟示例具有此输出:
> my_table
# A tibble: 4 × 5
product day1 day2 day3 colsum
<fctr> <int> <int> <int> <int>
1 apples 1 0 1 2
2 bananas 0 0 0 0
3 apples 2 0 4 6
4 rowsum 3 0 5 8
现在我删除最终值为零的行:
my_table = my_table %>%
filter(colsum > 0)
> my_table
# A tibble: 3 × 5
product day1 day2 day3 colsum
<fctr> <int> <int> <int> <int>
1 apples 1 0 1 2
2 apples 2 0 4 6
3 rowsum 3 0 5 8
问题是:
dplyr
中的值过滤列 我想做这样的事情:
# code that does NOT work
my_table = my_table %>%
filter(my_table[nrow(my_table)] > 0)
获取:
> my_table
# A tibble: 3 × 5
product day1 day3 colsum
<fctr> <int> <int> <int>
1 apples 1 1 2
2 apples 2 4 6
3 rowsum 3 5 8
更新:
@Patronius解决方案(适用于dplyr 0.5.0
)
my_table %>%
filter(colsum > 0) %>%
select_if(function(.) last(.) != 0)
# A tibble: 3 × 4
product day1 day3 colsum
<fctr> <int> <int> <int>
1 apples 1 1 2
2 apples 2 4 6
3 rowsum 3 5 8
答案 0 :(得分:5)
您可以使用dplyr&#39; RESULT=$(zenity --forms --title="Title"\
--text="Text"\
--add-entry="File Name"\
--add-entry="Directory")
NAME=$(echo $RESULT| cut -d '|' -f 1)
DIRECTORY=$(echo $RESULT| cut -d '|' -f 2)
if [ $DIRECTORY ]; then
COMMAND="$COMMAND $DIRECTORY "
fi
if [ $NAME ]; then
COMMAND="$COMMAND -name $NAME "
fi
find $COMMAND
和select_if
:
last
请注意,它保留了因子列my_table %>%
select_if(function(.) last(.) != 0)
(因为product
因子的最后一项为零是不正确的。)