0

我需要这方面的帮助,请。又挣扎了几个小时......

# open file for data connection
data <- file("data.log", open = "r")

# do readlines for the first
# there should be more than  500 lines for thousands of observations
rl <- readLines(data)

未包含在数据中的标签/标题:时间、组、任务、完成与否、小时、信用

[1]  10:00 A task1 comp 5:00 200                                                                
[2]  16:00 A task2 comp 3:00 100                                                                    
[3]  11:00 B task1 incomp 7:00 100                                                                                
[4]  17:00 B task2 comp 7:00 100                                                      
[5]  15:00 C task1 incomp 5:00 420                                               
[6]  19:00 C task2 comp 6:00 115  

所以我需要提取线条并将它们放在一起,如下所示:

[1]  10:00 A task1 comp 5:00 100                                                                
[2]  16:00 B task2 comp 3:00 100                                                                    
[3]  11:00 A task1 incomp 7:00 100                                                                                
[4]  17:00 B task2 comp 7:00 100                                                      
[5]  15:00 A task1 incomp 5:00 100                                               
[6]  19:00 B task2 comp 6:00 100 

我已经编写了一些函数,这是看起来最有可能运行但仍然出现错误的函数。

fuc = function(filename = "data.log")
{
  rl = readLines(data)
  spl = strsplit(rl[-1], " ") #split data with spaces
  pattern = grep("A.* 100", spl, value = TRUE) #grep the lines that have group A and credit 100

  if(pattern !=0) #if the pattern grep is different from 0
  {
    pattern2 = grep("B.*100", spl, value = TRUE) #grep lines that have group B and credit 100 that follows pattern above
    if(pattern2 !=0) #if pattern2 is different from 0
      print(pattern) 
  }

所以这个功能运行正常然后我用我的数据试试

fuc("data.log")

我得到错误:

Error in if (pattern != 0) { : argument is of length zero

我从其他人那里得到了建议,因为我已经对数据进行了拆分,所以我可以用类似的东西来 grep

grep(x[2] == "A" & x[6] == "100", x[2] == "B" & x[6] == "100", spl, value = TRUE)

但这不起作用......

请帮帮我

4

0 回答 0