【问题标题】:how to select a range of rows conditioned to start and end given certain text如何选择以给定特定文本开始和结束为条件的行范围
【发布时间】:2022-11-24 20:34:47
【问题描述】:

我有一个数据框,其格式如下所示。我想清理 df 留下一定范围的行,这些行在第 1 列说“国家”时开始,并在第 1 列中说“结束”之前结束两行。我需要那样,因为以后我必须将 df 与其他人绑定相同类型工作表的 dfs 但来自其他时期,因此工作表之间的范围不同。

Column A Column B Column C
- - -
country number year
china 1 2018
japan 2 2019
usa 3 2019
end
country number year
china 1 2018
japan 2 2019
usa 3 2019

我希望它看起来像这样,但它不适用于我一直尝试使用的代码:

  start_position <- which(df[,1]=="country")
  end_position <- which(df[,1]=="end")
  df<- df[df(start_position:(end_position-2)),]

请提供任何帮助或建议

【问题讨论】:

    标签: r


    【解决方案1】:
    start_position <- which(df[,1]=="country")
    end_position <- which(df[,1]=="end")
    # change the third line
    #         ___                               _       
    # df<- df[df(start_position:(end_position-2)),]
    #
    df <- df[ start_position:(end_position-2),]
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-12
      • 1970-01-01
      • 2021-11-06
      • 2020-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多