Biostar Beta. Not for public use.
how to separate/split numbers
0
Entering edit mode
2.9 years ago
Kian • 40
@Kian39484

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 • 472 views
ADD COMMENTlink
0
Entering edit mode
2.9 years ago
st.ph.n ♦ 2.5k
@st.ph.n10041

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 COMMENTlink
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 REPLYlink
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 REPLYlink
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 REPLYlink
0
Entering edit mode

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

ADD REPLYlink
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 REPLYlink
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 REPLYlink
0
Entering edit mode

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

ADD REPLYlink
0
Entering edit mode

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

ADD REPLYlink

Login before adding your answer.

Similar Posts
Loading Similar Posts
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.3