# How to remove starting and ending zeros in an R vector?

R ProgrammingServer Side ProgrammingProgramming

To remove starting and ending zeros in an R vector, we can use min and max function to access values except 0 and then subsetting with single square brackets. For example, if we have a vector called x then we can remove starting and ending zeros by using the command −

x[min(which(x!=0)):max(which(x!=0))]

## Example

Live Demo

x1<-c(rep(0,10),rpois(50,1),rep(0,10))
x1

## Output

[1] 0 0 0 0 0 0 0 0 0 0 0 0 1 0 3 0 0 1 1 0 0 0 0 1 0 0 1 0 0 4 2 1 0 0 0 0 0 0
[39] 1 0 2 0 0 2 4 1 1 0 2 0 0 1 2 1 1 0 3 0 1 0 0 0 0 0 0 0 0 0 0 0

## Example

x1[min(which(x1!=0)):max(which(x1!=0))]

## Output

[1] 1 0 3 0 0 1 1 0 0 0 0 1 0 0 1 0 0 4 2 1 0 0 0 0 0 0 1 0 2 0 0 2 4 1 1 0 2 0
[39] 0 1 2 1 1 0 3 0 1

## Example

Live Demo

x2<-c(rep(0,20),rpois(150,1),rep(0,20))
x2

## Output

  [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1 0 1 3 2 2 1 3 0 2 3 1
[38] 2 0 1 0 1 0 0 1 2 3 1 2 1 3 1 1 0 2 0 0 1 4 1 1 0 0 0 0 0 0 0 1 2 3 0 1 2
[75] 0 1 1 0 0 1 2 0 2 2 0 1 1 0 1 2 1 2 0 1 1 7 0 3 3 1 0 2 1 1 0 0 1 0 0 1 0
[112] 2 0 1 1 0 2 3 0 1 1 0 1 1 1 2 0 0 1 1 1 1 2 1 1 1 1 0 1 0 0 0 1 1 1 0 1 0
[149] 3 0 2 2 1 0 2 0 1 1 2 0 1 3 1 1 1 0 0 1 2 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[186] 0 0 0 0 0

## Example

x2[min(which(x2!=0)):max(which(x2!=0))]

## Output

[1] 1 0 1 1 1 0 1 3 2 2 1 3 0 2 3 1 2 0 1 0 1 0 0 1 2 3 1 2 1 3 1 1 0 2 0 0 1
[38] 4 1 1 0 0 0 0 0 0 0 1 2 3 0 1 2 0 1 1 0 0 1 2 0 2 2 0 1 1 0 1 2 1 2 0 1 1
[75] 7 0 3 3 1 0 2 1 1 0 0 1 0 0 1 0 2 0 1 1 0 2 3 0 1 1 0 1 1 1 2 0 0 1 1 1 1
[112] 2 1 1 1 1 0 1 0 0 0 1 1 1 0 1 0 3 0 2 2 1 0 2 0 1 1 2 0 1 3 1 1 1 0 0 1 2
[149] 5

## Example

Live Demo

x3<-c(rep(0,20),rpois(150,8),rep(0,20))
x3

## Output

  [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 5 6 8 5
[26] 11 6 7 7 15 5 15 12 9 5 5 5 6 4 10 10 5 5 5 8 3 12 6 7 12
[51] 8 13 8 6 12 11 15 6 12 9 9 3 3 6 7 7 11 6 3 5 12 11 6 6 13
[76] 8 8 9 9 5 10 9 7 8 6 10 5 10 8 7 8 4 8 10 8 12 3 10 8 7
[101] 7 5 11 18 7 7 9 14 5 12 3 10 6 12 5 3 5 7 8 10 9 3 13 11 10
[126] 6 5 5 9 9 9 7 4 8 5 10 11 3 10 10 10 11 8 12 7 11 10 4 9 3
[151] 5 2 7 8 8 10 9 12 7 12 8 9 7 7 6 13 11 7 6 10 0 0 0 0 0
[176] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

## Example

x3[min(which(x3!=0)):max(which(x3!=0))]

## Output

[1] 5 5 6 8 5 11 6 7 7 15 5 15 12 9 5 5 5 6 4 10 10 5 5 5 8
[26] 3 12 6 7 12 8 13 8 6 12 11 15 6 12 9 9 3 3 6 7 7 11 6 3 5
[51] 12 11 6 6 13 8 8 9 9 5 10 9 7 8 6 10 5 10 8 7 8 4 8 10 8
[76] 12 3 10 8 7 7 5 11 18 7 7 9 14 5 12 3 10 6 12 5 3 5 7 8 10
[101] 9 3 13 11 10 6 5 5 9 9 9 7 4 8 5 10 11 3 10 10 10 11 8 12 7
[126] 11 10 4 9 3 5 2 7 8 8 10 9 12 7 12 8 9 7 7 6 13 11 7 6 10

## Example

Live Demo

x4<-c(rep(0,20),rpois(150,50),rep(0,20))
x4

## Output

  [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 39 63 39 53 62
[26] 43 40 41 50 42 48 50 59 52 47 49 49 52 48 54 46 59 43 45 41 57 56 54 48 43
[51] 47 47 53 51 44 54 56 49 55 42 64 48 47 46 47 51 52 32 39 49 47 59 39 50 54
[76] 46 52 46 61 40 58 56 50 56 66 55 60 54 56 39 55 41 50 53 56 51 53 52 53 50
[101] 58 43 42 42 53 54 50 59 53 47 61 47 62 62 45 60 45 49 59 46 57 48 55 55 47
[126] 47 61 55 55 50 61 45 60 52 51 32 47 47 61 43 42 61 58 44 51 55 46 51 46 42
[151] 40 43 54 41 63 52 48 41 43 70 47 55 45 54 47 57 56 54 53 49 0 0 0 0 0
[176] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

## Example

x4[min(which(x4!=0)):max(which(x4!=0))]

## Output

[1] 39 63 39 53 62 43 40 41 50 42 48 50 59 52 47 49 49 52 48 54 46 59 43 45 41
[26] 57 56 54 48 43 47 47 53 51 44 54 56 49 55 42 64 48 47 46 47 51 52 32 39 49
[51] 47 59 39 50 54 46 52 46 61 40 58 56 50 56 66 55 60 54 56 39 55 41 50 53 56
[76] 51 53 52 53 50 58 43 42 42 53 54 50 59 53 47 61 47 62 62 45 60 45 49 59 46
[101] 57 48 55 55 47 47 61 55 55 50 61 45 60 52 51 32 47 47 61 43 42 61 58 44 51
[126] 55 46 51 46 42 40 43 54 41 63 52 48 41 43 70 47 55 45 54 47 57 56 54 53 49

## Example

Live Demo

x5<-c(rep(0,20),rpois(150,200),rep(0,20))
x5

## Output

  [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[19] 0 0 191 203 207 200 173 206 226 195 210 181 196 210 227 198 211 212
[37] 193 214 204 212 220 171 218 209 219 201 191 179 207 197 181 204 195 213
[55] 202 202 206 173 211 202 219 191 206 184 225 204 219 200 221 183 186 202
[73] 211 200 199 214 180 186 211 215 212 213 225 199 206 189 228 206 221 176
[91] 194 200 185 182 213 182 186 178 216 204 211 178 195 212 210 222 185 192
[109] 229 203 234 204 197 200 189 176 197 195 198 204 195 208 195 178 203 227
[127] 196 197 200 187 200 178 210 213 204 194 213 211 202 225 185 201 165 223
[145] 218 214 205 205 191 198 228 195 189 212 211 163 178 194 198 171 187 191
[163] 208 202 192 195 213 186 194 187 0 0 0 0 0 0 0 0 0 0
[181] 0 0 0 0 0 0 0 0 0 0

## Example

x5[min(which(x5!=0)):max(which(x5!=0))]

## Output

[1] 191 203 207 200 173 206 226 195 210 181 196 210 227 198 211 212 193 214
[19] 204 212 220 171 218 209 219 201 191 179 207 197 181 204 195 213 202 202
[37] 206 173 211 202 219 191 206 184 225 204 219 200 221 183 186 202 211 200
[55] 199 214 180 186 211 215 212 213 225 199 206 189 228 206 221 176 194 200
[73] 185 182 213 182 186 178 216 204 211 178 195 212 210 222 185 192 229 203
[91] 234 204 197 200 189 176 197 195 198 204 195 208 195 178 203 227 196 197
[109] 200 187 200 178 210 213 204 194 213 211 202 225 185 201 165 223 218 214
[127] 205 205 191 198 228 195 189 212 211 163 178 194 198 171 187 191 208 202
[145] 192 195 213 186 194 187
Published on 16-Mar-2021 11:29:55