how to separate/split numbers
1
0
Entering edit mode
6.7 years ago
Kian ▴ 50

I have a matrix with two digit numbers like 11, i want to split it in two columns, like 1 1

   00 00
   11 22
to
  0 0 0 0 
  1 1 2 2

What's the best way to do this in R? the matrix is large and has 3000 rows with 50000 columns.

R MATRIX COLUMNS NUMBER • 1.9k views
ADD COMMENT
0
Entering edit mode
6.7 years ago
st.ph.n ★ 2.7k

You specifically said R, but didn't show an attempt at achieving the desired output, or mention that it was part of a larger R script. If this is an assignment, please make an attempt to solve it. So, here's a Python solution (assuming no headers, and that all values are space delimited):

Save as split_mat.py

#!/usr/bin/env python
import sys

with open(sys.argv[1], 'r') as f:
        for line in f:
                print ' '.join(''.join(i for i in line.strip().split(' ')))

Input:

00 00
11 22

Output:

0 0 0 0
1 1 2 2

Run:

python split_mat.py input.txt > output.txt
ADD COMMENT
0
Entering edit mode

Thanks for your response, its better i do it in R. do you know how its work in R? also to matrix have colnames and row names too.

ADD REPLY
0
Entering edit mode

Why is it better to do it in R? The column headers, and first entry in each row, can easily be printed to the output.

ADD REPLY
0
Entering edit mode

Ok Thanks, im not familiar with python.can you tell me, how i import my file to python and next run the program and then write the results.

ADD REPLY
0
Entering edit mode

also my file have column header and my first column is id that i dont want to change.

ADD REPLY
0
Entering edit mode

there is an error after run code!

File "<stdin>", line 3 print ' '.join(''.join(i for i in line.strip().split(' '))) ^ SyntaxError: invalid syntax

ADD REPLY
0
Entering edit mode

Check your version of python. If you have python 3+, change the print line to have parentheses around the statement, print()

If one column is split into two, how will you handle the headers?

ADD REPLY
0
Entering edit mode

i want to new column have the same name as origin column.

ADD REPLY
0
Entering edit mode

Print() is worked, but python split_mat.py input.txt > output.txt have problem yet.

ADD REPLY

Login before adding your answer.

Traffic: 2564 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6