Merge two text files line by line in Ruby - ruby

I'm trying to figure out how to merge two text files line by line. The letters file contains letters in a column A to I. Numbers contains numbers in a column from 1 to 9. This is what I have so far:
file='C:\\Users\\USERNAME\\Desktop\\numbers.txt'
f = File.open(file, "r")
f.each_line { |line|
dile='C:\\Users\\USERNAME\\Desktop\\letters.txt'
d = File.open(dile, "r")
d.each_line { |dine|
this = line + dine
print this
}
}
But my results are like this:
1
A
1
B
1
C
1
D
1
E
1
F
1
G
1
H
1
I
1
J2
A
2
B
2
C
2
D
2
E
2
F
2
G
2
H
2
I
2
J3
A
3
B
3
C
3
D
3
E
3
F
3
G
3
H
3
I
3
J4
A
4
B
4
C
4
D
4
E
4
F
4
G
4
H
4
I
4
J5
A
5
B
5
C
5
D
5
E
5
F
5
G
5
H
5
I
5
J6
A
6
B
6
C
6
D
6
E
6
F
6
G
6
H
6
I
6
J7
A
7
B
7
C
7
D
7
E
7
F
7
G
7
H
7
I
7
J8
A
8
B
8
C
8
D
8
E
8
F
8
G
8
H
8
I
8
J9
A
9
B
9
C
9
D
9
E
9
F
9
G
9
H
9
I
9
J10A
10B
10C
10D
10E
10F
10G
10H
10I
10J
When what I really want is something like this:
1A
2B
3C
4D
5E
6F
7G
8H
9I
Anyone have any idea how to do this?

f1, f2 = [
'C:\\Users\\USERNAME\\Desktop\\numbers.txt',
'C:\\Users\\USERNAME\\Desktop\\letters.txt'
]
File.readlines(f1).map(&:chomp)
.zip(File.readlines(f2).map(&:chomp))
.map(&:join)
or, without double chomping:
File.readlines(f1).zip(File.readlines(f2))
.map(&:join)
.map { |s| s.gsub /#$//, '' }

Its because each line already carries line feed \n. Try using chomp:
this = line.chomp + dine.chomp

Like a #mudasobwa answer
=> File.readlines('num').zip(File.readlines('let')).flat_map { |x| x.map(&:chomp!).join }
=> [
[0] "1A",
[1] "2B",
[2] "3C",
[3] "4D",
[4] "5E"
]
just without double chomp

Related

Find the smallest sub square matrix that contains all the vowels

You are provided with 2-D Matrix, A, such that
1. A (MxN), is 2-Dimensional Matrix
2. A[i][j] E [A-Z]
you have to find the smallest sub square matrix, such that
it contains all the vowels i.e. {A E, I, O, U}.
Input: A[][] ->
A E I O B 1 2 3 4 5
C D U Z O 6 7 8 9 10
P O A E K 11 12 13 14 15
B R E A K 16 17 18 19 20
Possible Outputs:
A E I 1 2 3
C D U 5 6 7
P O A 9 10 11
I O B 3 4 5
U Z O 8 9 10
A E K 13 14 15
so, size here will be 3x3 square matrix, so it is 3.
Please help me to find out the logic behind this problem.

converting 3 variable into a matrix form to create a heatmap in SAS

I'm trying to convert 3 vairables into a matrix, for expample if you have the following:
(CHAR) (char) (num)
Var1 Var2 Var3
A B 1
C D 2
E F 3
A D 4
A F 5
C B 6
C F 7
E B 8
E D 9
Any ideas on how to convert the above three variables into this form of matrix below and my goal is to construct a heatmap using this matix
B D F
A 1 4 5
C 6 2 7
E 8 9 3
Can anyone help me do this in SAS, either using SAS/IML or other Procedure? Thanks!
Assuming you are using a recent version of SAS/IML (13.1 or later), use the HEATMAPCONT or HEATMAPDISC call:
proc iml;
m = {1 4 5,
6 2 7,
8 9 3};
call heatmapcont(m) xvalues={B D F} yvalues={A C E};
For details, see Creating heat maps in SAS/IML
It will be better if you post your code first then ask questions.
I think proc transpose is the fastest solution.
data _t1;
input var1 $ var2 $ var3 5.;
cards;
A B 1
C D 2
E F 3
A D 4
A F 5
C B 6
C F 7
E B 8
E D 9
run;
proc sort data=_t1;by var1;run;
proc transpose data=_t1 out=_t2(drop=_name_ rename=(var1=HereUpToYou));
by var1;
var var3;
id var2;
run;

How to compute a natural join??? 5

Table R (A, C) contains the following entries:
A C
3 3
6 4
2 3
3 5
7 1
Table S (B, C, D) following
B C D
5 1 6
1 5 8
4 3 9
Calculate the natural join of R and S. Which of the lines would be the result? Each resulting string has the following schema (A, B, C, D).
Please help!!!
Got the answer by looking at this. So your answer should be: {(3,4,3,9),(2,4,3,9),(3,1,5,8),(7,5,1,6)}
A B C D
3 4 3 9
2 4 3 9
3 1 5 8
7 5 1 6

Which function/algorithm for this merging and filling operation?

I have written R code that merges two data frames based on first column and for missing data adds the value from above. Here is what is does:
Two input data frames:
1 a
2 b
3 c
5 d
And
1 e
4 f
6 g
My code gives this output:
1 a e
2 b e
3 c e
4 c f
5 d f
6 d g
My code is however inefficient as it is not vectorized properly. Are there some R functions which I could use? Basically a function I am looking for is that fills in missing values / NA values and takes the value from previous element and puts it in place of NA.
I looked through reference book of R, but could not find anything.
Here is a solution making use of zoo::na.locf
library(zoo)
a <- data.frame(id=c(1,2,3,5), v=c("a","b","c", "d"))
b <- data.frame(id=c(1,4,6), v=c("e", "f", "g"))
n <- max(c(a$id, b$id))
an <- merge(data.frame(id=1:n), a, all.x=T)
bn <- merge(data.frame(id=1:n), b, all.x=T)
an$v <- na.locf(an$v)
bn$v <- na.locf(bn$v)
data.frame(an$id, an$v, bn$v)
an.id an.v bn.v
1 1 a e
2 2 b e
3 3 c e
4 4 c f
5 5 d f
6 6 d g

How to sort dataframe in R with specified column order preservation?

Let's say I have a data.frame
x <- data.frame(a = c('A','A','A','A','A', 'C','C','C','C', 'B','B','B'),
b = c('a','c','a','a','c', 'd', 'e','e','d', 'b','b','b'),
c = c( 7, 3, 2, 4, 5, 3, 1, 1, 5, 5, 2, 3),
stringsAsFactors = FALSE)
> x
a b c
1 A a 7
2 A c 3
3 A a 2
4 A a 4
5 A c 5
6 C d 3
7 C e 1
8 C e 1
9 C d 5
10 B b 5
11 B b 2
12 B b 3
I would like to sort x by columns b and c but keeping order of a as before. x[order(x$b, x$c),] - breaks order of column a. This is what I want:
a b c
3 A a 2
4 A a 4
1 A a 7
2 A c 3
5 A c 5
6 C d 3
9 C d 5
7 C e 1
8 C e 1
11 B b 2
12 B b 3
10 B b 5
Is there a quick way of doing it?
Currently I run "for" loop and sort each subset, I'm sure there must be a better way.
Thank you!
Ilya
If column "a" is ordered already, then its this simple:
> x[order(x$a,x$b, x$c),]
a b c
3 A a 2
4 A a 4
1 A a 7
2 A c 3
5 A c 5
6 B d 3
9 B d 5
7 B e 1
8 B e 1
11 C b 2
12 C b 3
10 C b 5
If column a isn't ordered (but is grouped), create a new factor with the levels of x$a and use that.
Thank you Spacedman! Your recommendation works well.
x$a <- factor(x$a, levels = unique(x$a), ordered = TRUE)
x[order(x$a,x$b, x$c),]
Following Gavin's comment
x$a <- factor(x$a, levels = unique(x$a))
x[order(x$a,x$b, x$c),]
require(doBy)
orderBy(~ a + b + c, data=x)

Resources