Skip to content

Instantly share code, notes, and snippets.

@hiroyuki-sato
Last active January 7, 2016 13:22
Show Gist options
  • Save hiroyuki-sato/cb36584f6cd5845b6c3e to your computer and use it in GitHub Desktop.
Save hiroyuki-sato/cb36584f6cd5845b6c3e to your computer and use it in GitHub Desktop.
GNU R Question in English

Requirements

  • I would like to compare two data.
  • How to select columns which has non-zero datas?.

Sample Data

Real data has 5000 columns.

sample1.txt

ID VAL1 VAL2 VAL3
ID1 0 2 3
ID2 0 2 3
ID3 0 2 3
ID,VAL1,VAL2,VAL3
ID1,0,2,3
ID2,0,2,3
ID3,0,2,3

sample2.txt

ID VAL1 VAL2 VAL3
ID1 0 2 3
ID2 0 2 3
ID3 0 2 2
ID,VAL1,VAL2,VAL3
ID1,0,2,3
ID2,0,2,3
ID3,0,2,2

Compare results

sample1 - sample2

ID VAL1 VAL2 VAL3
ID1 0 0 0
ID2 0 0 0
ID3 0 0 1

select columns which has only non-zero data column like the following

ID VAL3
ID1 0
ID2 0
ID3 1

R code

Read frame

sample1 <- read.table("sample1.txt",header=T,sep=',')
sample2 <- read.table("sample2.txt",header=T,sep=',')

comapre

result <- sample1[,2:4] - sample2[,2:4]
result
  VAL1 VAL2 VAL3
1    0    0    0
2    0    0    0
3    0    0    1

How to select VAL3 columns?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment