How do I skip “#” sign without loop while extract elements?

后端 未结 2 1941
[愿得一人]
[愿得一人] 2021-01-24 05:05

I want to get a new data.frame from this data set,but there are some description with\"#\" between some rows and some rows contain \"#\" sign, I can use \"for\" loop under the c

2条回答
  •  深忆病人
    2021-01-24 05:29

    First, remove the rows starting with # from your one-column data frame x:

    vec <- grep("^[^#]", x[[1]], value = TRUE)
    

    Then, create a new data frame based on the remaining data:

    data.frame(V1 = gsub("(.*\\:[0-9]+) .*", "\\1", vec),
               V2 = gsub(".* ([0-9]+) [0-9]+ [0-9]+ *$", "\\1", vec))
    
    #                    V1  V2
    # 1 2013-08-27 16:00:00 200
    # 2 2013-08-27 16:00:01 200
    # 3 2013-08-27 16:00:02 200
    # 4 2013-08-27 16:00:03 200
    

提交回复
热议问题