Skip to content
Back to formatted view

Raw Message

Message-ID: <BAY176-W344E0B16685F17D2A3E912C0D00@phx.gbl>
Date: 2014-08-22T12:18:04Z
From: James Wei
Subject: print vectors with consecutive numbers

Hi all,

I have a matrix with consecutive and non-consecutive numbers
in columns. For example, the first 2 columns have consecutive numbers. I want R
to print only columns with consecutive numbers. Here is the matrix and how I
did using conditional statement: 

##

mat=matrix(data=c(9,2,3,4,5,6,10,13,15,17,19,22,
25,27,29,31,34,37,39,41),ncol=5)

mat

difference = diff(mat)==1

difference

y1=difference[1,]

y2=difference[2,]

y3=difference[3,]

y=(y1|y2|y3)

y

 

if (y=="TRUE") mat else 0

## 

However, R still print all 5 columns, not the first 2
columns I wanted. I got the Warning message:

In if (y == "TRUE") mat else 0 :

  the condition has
length > 1 and only the first element will be used 

How can I change the code to get only the first 2 columns
with consecutive numbers printed? I am new to R.  

Thanks in advance for your help. James  

 		 	   		  
	[[alternative HTML version deleted]]