r語言,csv數(shù)據(jù),提取特定行
sample | TCGA-CA-5256-01 | TCGA-AZ-6599-11 | TCGA-AA-3655-01 | TCGA-A6-6137-01 | TCGA-CK-4952-01 | TCGA-A6-5657-01 | TCGA-AD-6963-01 | TCGA-AA-3663-11 |
ARHGEF10L | 10.1616 | 11.1212 | 11.0245 | 11.0576 | 10.566 | 10.4189 | 10.8635 | 11.0543 |
HIF3A | 3.7172 | 2.3437 | 2.0858 | 6.0759 | 1.9506 | 5.4777 | 4.4634 | 8.4492 |
RNF17 | 0 | 0 | 0.5495 | 0 | 0 | 0 | 0 | 0 |
上面第一行顯示樣本名字,樣本名字最后的兩個數(shù)字:01代表癌組織,11代表正常組織,有辦法只提取正常組織的列嗎?萬分感謝
2018-08-30
剛學(xué)R不太會,用python寫的把csv文件里你的要求寫到一個新的csv文集里
import re
input_file=open('yourfile.csv')
output_file=open('result.csv','w')
table=[]
for line in input_file:
? ? line=line.strip().split(',')
? ? table.append(line)
new_table=zip(*table)
pattern=re.compile(".*-11")
result_table=[]
for line in new_table:
? ? match=pattern.match(line[0])
? ? if match:
? ? ? ? result_table.append(line)
result_table=zip(*result_table)
for line in result_table:
? ? line=','.join(line)+'\n'
? ? output_file.write(line)
output_file.close()
input_file.close()