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.
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.
1 answer
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
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.
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.
also my file have column header and my first column is id that i dont want to change.
there is an error after run code!
File "<stdin>", line 3 print ' '.join(''.join(i for i in line.strip().split(' '))) ^ SyntaxError: invalid syntax
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?
Log in to answer this question.