Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201919

How to count the frequency of lines in a file

$
0
0

I have the following txt file:

Test-case: 1 --------------------
int int0 = (-1790);
String string0 = "14_d4BWMJqn";
MethodVisitor methodVisitor0 = mock(MethodVisitor.class, new ViolatedAssumptionAnswer());
MethodAdapter methodAdapter0 = new MethodAdapter(methodVisitor0);
int int1 = 4073;
Label label0 = new Label();
methodAdapter0.visitLabel(label0);
String string1 = "H*";
String string2 = "5^?5P\"#V\"c<_yB";
methodAdapter0.visitFieldInsn(int1, string0, string1, string2);
LocalVariablesSorter localVariablesSorter0 = new LocalVariablesSorter(int0, string0, methodAdapter0);
localVariablesSorter0.visitVarInsn(int0, int0);
Attribute attribute0 = mock(Attribute.class, new ViolatedAssumptionAnswer());
methodAdapter0.visitAttribute(attribute0);
int int2 = (-2549);
int int3 = 4149;
localVariablesSorter0.visitVarInsn(int2, int3);
int int0 = (-1790);
localVariablesSorter0.visitMaxs(int2, int2);

I would like to count how many each line appears in the text file (the occurrences of each line in the file). To do that:

  con = file("/home/adam/Desktop/Trash/qwe.txt", "r")
  lines <- readLines(con)
  data_per_class1 <- data.frame(Statement = NA, Freq = NA)
  x <- 1
  for(i in 1:length(lines)){
    frq <- length(grep(lines[i], lines))
    data_per_class1[x,] <- c(lines[i], frq)
    x <- x + 1
  }
  print(data_per_class1)
  close(con)

The results that I got is:

enter image description here

The results are wrong. Each line should at least has frequency of 1. Also, there is one line that has frequency of 2 which is int int0 = (-1790); but it is given frequency of zero. I think the problem with this grep(lines[i], lines)

Can you please help solving this issue?


Viewing all articles
Browse latest Browse all 201919

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>