-1

I want to compare each element of column 2 with an increasing value 1 to 10. If it matches, I want to count and print them in a new file with tab in between. Example:

X   Y
1   1
2   1
3   1
4   2
5   2
6   2
7   4
8   4
9   4
10  4

I want to check if Y column matches with 1 (in this case it matches 3 times) So I want output to be printed as follows in a file output.txt

output.txt
[result]
1    2   3

Next want to look for Y column with value 2 (in this case I have 3 matches) So want to append the count to output.txt

output.txt
[result]
1    2   3
4    5   6

Next want to look for Y column with value 4 (in this case I have 4 matches) So want to append the count to output.txt

output.txt
[result]
1    2   3
4    5   6
7    8   9   10

I am beginner and would appreciate you kind help.

Thanks and regards, Bikash

thelatemail
  • 91,185
  • 12
  • 128
  • 188
Bikash
  • 11
  • 2
  • can you include a code sample of something you've tried? (see [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for format) – lefft Feb 19 '18 at 02:57

2 Answers2

0

I'm not sure I get your question but you want output.txt to act like a counter right? I.e. the last numeric value in it should indicate the last line of the file? You could use writeLines to do that:

## The example data (with nothing in column X)
Y <- c(1, 1, 2, 1, 3, 1, 4, 2, 5, 2, 6, 2, 7, 4, 8, 4, 9, 4, 10, 4)
X <- NA

## Making a data frame
data_frame <- data.frame(cbind(X, Y))

## Selecting the unique elements in Y
unique_Y <- unique(data_frame$Y)

## Create the output file
connection_output <- file("output.txt", "w")

## Initialise the position where to write the text
position <- 0

## Looping through each unique element
for(row in unique_Y) {

    ## Get the occurences of the variable row
    matches <- which(data_frame$Y == row)

    ## Append the text
    writeLines(as.character(position + 1:length(matches)), con = connection_output, sep = " ")

    ## Update the counter
    position <- position + length(matches)
}

## Close the connection
close(connection_output)

This will print in output.txt:

1 2 3 4

(the sequence from 1 to the number of 1s in Y - 4 in your example). Then will be appended as:

1 2 3 4 5 6 7 8

(the sequence from 5 to the number of 2s in Y - 4 in your example). Then:

1 2 3 4 5 6 7 8 9

(the sequence from 9 to the number of 3s in Y - 1 in your example). Etc.

Thomas Guillerme
  • 1,747
  • 4
  • 16
  • 23
0

You can split up your X column by the values in Y, and then send out to a file using cat:

cat(sapply(split(dat$X, dat$Y), paste, collapse="\t"), sep="\n")
#1       2       3
#4       5       6
#7       8       9       10
thelatemail
  • 91,185
  • 12
  • 128
  • 188