Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.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中某个值的子集来创建多个数据帧_R_Function_Dataframe_Filter_Lapply - Fatal编程技术网

通过筛选不等于R中某个值的子集来创建多个数据帧

通过筛选不等于R中某个值的子集来创建多个数据帧,r,function,dataframe,filter,lapply,R,Function,Dataframe,Filter,Lapply,我有一个带有两列文本和颜色的数据框 图书馆管理员 图书馆咕噜声 示例数据帧 df在函数内部,是颜色而不是颜色 如果我们需要data.frame的列表,而不是map_df,只需使用map 或者将函数更改为 remaining_colours <- function(x) { df %>% filter(!Colours == x) } 在功能内部,是颜色而不是颜色 如果我们需要data.frame的列表,而不是map_df,只需使用map 或者将函

我有一个带有两列文本和颜色的数据框

图书馆管理员 图书馆咕噜声 示例数据帧
df在函数内部,是颜色而不是颜色

如果我们需要data.frame的列表,而不是map_df,只需使用map

或者将函数更改为

remaining_colours <- function(x) {

     df %>% 
         filter(!Colours == x)
  }

在功能内部,是颜色而不是颜色

如果我们需要data.frame的列表,而不是map_df,只需使用map

或者将函数更改为

remaining_colours <- function(x) {

     df %>% 
         filter(!Colours == x)
  }
这里有一种使用lappy的方法。这将创建所需数据帧的列表

colours <- c("blue", "white", "green", "yellow")

result <- lapply(colours, function(x) {
             df %>% filter(!Colours == x)
          }) %>% 
          setNames(paste0("NOT_", colours))

result

$NOT_blue
   Text Colours
1 text2   white
2 text3   green
3 text4  yellow

$NOT_white
   Text Colours
1 text1    blue
2 text3   green
3 text4  yellow

$NOT_green
   Text Colours
1 text1    blue
2 text2   white
3 text4  yellow

$NOT_yellow
   Text Colours
1 text1    blue
2 text2   white
3 text3   green
这里有一种使用lappy的方法。这将创建所需数据帧的列表

colours <- c("blue", "white", "green", "yellow")

result <- lapply(colours, function(x) {
             df %>% filter(!Colours == x)
          }) %>% 
          setNames(paste0("NOT_", colours))

result

$NOT_blue
   Text Colours
1 text2   white
2 text3   green
3 text4  yellow

$NOT_white
   Text Colours
1 text1    blue
2 text3   green
3 text4  yellow

$NOT_green
   Text Colours
1 text1    blue
2 text2   white
3 text4  yellow

$NOT_yellow
   Text Colours
1 text1    blue
2 text2   white
3 text3   green

非常感谢!如何将结果导出/写入csv文件?@yiah you can do-lapplyseq_alongresult,functionf{write.csvresult[[f]],file=paste0namesresult[f],.csv,row.names=f}非常感谢!如何将结果导出/写入csv文件?@yiah you can-do-lapplyseq_alongresult,functionf{write.csvresult[[f]],file=paste0namesresult[f],.csv,row.names=f}非常感谢!两种解决方案都是有效的。在我的例子中,map_df可能更方便,因为所有内容都指向一个data.frame。在这种情况下,我需要一个不匹配颜色映射的列,~df%>%filterColours!=。x%>%突变不匹配颜色=.x非常感谢!两种解决方案都是有效的。在我的例子中,map_df可能更方便,因为所有内容都指向一个data.frame。在这种情况下,我需要一个不匹配颜色映射的列,~df%>%filterColours!=。x%>%突变不匹配颜色=.x