Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从r中的数据帧中删除每N列_R_Dataframe - Fatal编程技术网

从r中的数据帧中删除每N列

从r中的数据帧中删除每N列,r,dataframe,R,Dataframe,我试图通过删除每三列来减小数据帧的大小 以下是我的示例数据帧: example = data.frame(x=c(1,2,3,4), y=c(1,2,3,4), z=c(1,2,3,4), w=c(1,2,3,4), p=c(1,2,3,4), q=c(1,2,3,4), r=c(1,2,3,4)) 看起来像这样 x y z w p q r 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 x y w p r 1 1 1 1 1

我试图通过删除每三列来减小数据帧的大小

以下是我的示例数据帧:

example = data.frame(x=c(1,2,3,4), y=c(1,2,3,4), z=c(1,2,3,4), w=c(1,2,3,4), p=c(1,2,3,4), q=c(1,2,3,4), r=c(1,2,3,4))
看起来像这样

x y z w p q r
1 1 1 1 1 1 1
2 2 2 2 2 2 2
3 3 3 3 3 3 3
4 4 4 4 4 4 4
x y w p r
1 1 1 1 1 
2 2 2 2 2 
3 3 3 3 3 
4 4 4 4 4 
我想把它转换成这样的东西

x y z w p q r
1 1 1 1 1 1 1
2 2 2 2 2 2 2
3 3 3 3 3 3 3
4 4 4 4 4 4 4
x y w p r
1 1 1 1 1 
2 2 2 2 2 
3 3 3 3 3 
4 4 4 4 4 
我已经能够使用tidyverse减少行数:

example <- example %>% dplyr::filter(row_number() %% 3 != 1) 
但是我不断地得到这个错误: c%%3中出错:二进制运算符的非数值参数

提前感谢您的帮助。

这是:

library(dplyr)

col_index <- seq(1:ncol(example)) 
example %>% select(col_index[col_index %% 3 != 0]) 
库(dplyr)
col\u index%select(col\u index%%3!=0])

您可以在base中以非常简单的方式执行此操作

示例[,c(真、真、假)]
逻辑向量将根据需要为列重复。如果你想扩大规模,你可以这样做

n%
选择(所有内容()[c(真、真、假)])