# How to remove some last elements of a vector in R?

A vector in R can have infinite number of elements but we might want to remove some of them. To remove the last elements of a vector, we can use head function with negative sign of the number of values we do not want. For example, if we have a vector of length 200 but we don’t want last fifty elements then we can use head(vector_name,-50).

## Examples

x1<-rnorm(100)
x1

## Output

[1] 0.19166338 1.55254427 0.91424229 0.35862537 0.17509564 -0.84726777
[7] 0.97823166 1.80586826 0.12291480 -0.12977203 -0.21642866 1.44647817
[13] 0.40970980 0.91091657 1.43035817 -0.38129196 0.20230718 -0.80619919
[19] 0.29463418 1.40488308 1.02376685 0.47612606 -0.67033033 0.15923432
[25] -0.38271538 0.93576259 -0.63153227 -0.09830608 1.03198498 0.38780843
[31] -1.25612931 -0.78695273 0.42981155 -0.37641622 -1.21622907 1.02927851
[37] 0.43039700 -1.24557402 -0.60272849 0.66006939 2.05074953 0.49080818
[43] -1.73147942 0.71088366 0.01382291 -1.40104160 1.25912367 -0.12747752
[49] -0.72938651 -1.21136136 0.59961974 -1.16032953 0.43909343 0.20485374
[55] -0.69918134 -0.92662568 -1.01348238 0.60498706 1.73440013 -0.34985053
[61] 1.19918551 1.02390100 -0.04627515 1.38650843 -2.19527272 -0.12755691
[67] -1.31699684 -1.42693514 0.87944163 0.39399158 0.68892548 2.35371650
[73] -1.01509767 0.29097752 1.56532608 -0.84711837 1.32766016 0.47704633
[79] 0.18525094 0.25329489 2.59222735 1.07434699 -1.59690189 -0.08758860
[85] 0.36076759 -0.87995998 -3.32333496 -0.46751545 0.43154027 -0.60398945
[91] 0.67444669 0.63592054 -0.61297039 0.41489135 0.87734337 0.02115758
[97] 1.81038329 -0.45209570 -0.12512399 0.76680042

## Example

head(x1,-50)

## Output

[1] 0.19166338 1.55254427 0.91424229 0.35862537 0.17509564 -0.84726777
[7] 0.97823166 1.80586826 0.12291480 -0.12977203 -0.21642866 1.44647817
[13] 0.40970980 0.91091657 1.43035817 -0.38129196 0.20230718 -0.80619919
[19] 0.29463418 1.40488308 1.02376685 0.47612606 -0.67033033 0.15923432
[25] -0.38271538 0.93576259 -0.63153227 -0.09830608 1.03198498 0.38780843
[31] -1.25612931 -0.78695273 0.42981155 -0.37641622 -1.21622907 1.02927851
[37] 0.43039700 -1.24557402 -0.60272849 0.66006939 2.05074953 0.49080818
[43] -1.73147942 0.71088366 0.01382291 -1.40104160 1.25912367 -0.12747752
[49] -0.72938651 -1.21136136

## Example

head(x1,-10)

## Output

[1] 0.19166338 1.55254427 0.91424229 0.35862537 0.17509564 -0.84726777
[7] 0.97823166 1.80586826 0.12291480 -0.12977203 -0.21642866 1.44647817
[13] 0.40970980 0.91091657 1.43035817 -0.38129196 0.20230718 -0.80619919
[19] 0.29463418 1.40488308 1.02376685 0.47612606 -0.67033033 0.15923432
[25] -0.38271538 0.93576259 -0.63153227 -0.09830608 1.03198498 0.38780843
[31] -1.25612931 -0.78695273 0.42981155 -0.37641622 -1.21622907 1.02927851
[37] 0.43039700 -1.24557402 -0.60272849 0.66006939 2.05074953 0.49080818
[43] -1.73147942 0.71088366 0.01382291 -1.40104160 1.25912367 -0.12747752
[49] -0.72938651 -1.21136136 0.59961974 -1.16032953 0.43909343 0.20485374
[55] -0.69918134 -0.92662568 -1.01348238 0.60498706 1.73440013 -0.34985053
[61] 1.19918551 1.02390100 -0.04627515 1.38650843 -2.19527272 -0.12755691
[67] -1.31699684 -1.42693514 0.87944163 0.39399158 0.68892548 2.35371650
[73] -1.01509767 0.29097752 1.56532608 -0.84711837 1.32766016 0.47704633
[79] 0.18525094 0.25329489 2.59222735 1.07434699 -1.59690189 -0.08758860
[85] 0.36076759 -0.87995998 -3.32333496 -0.46751545 0.43154027 -0.60398945

## Example

head(x1,-90)

## Output

[1] 0.1916634 1.5525443 0.9142423 0.3586254 0.1750956 -0.8472678
[7] 0.9782317 1.8058683 0.1229148 -0.1297720

## Example

head(x1,-30)

## Output

[1] 0.19166338 1.55254427 0.91424229 0.35862537 0.17509564 -0.84726777
[7] 0.97823166 1.80586826 0.12291480 -0.12977203 -0.21642866 1.44647817
[13] 0.40970980 0.91091657 1.43035817 -0.38129196 0.20230718 -0.80619919
[19] 0.29463418 1.40488308 1.02376685 0.47612606 -0.67033033 0.15923432
[25] -0.38271538 0.93576259 -0.63153227 -0.09830608 1.03198498 0.38780843
[31] -1.25612931 -0.78695273 0.42981155 -0.37641622 -1.21622907 1.02927851
[37] 0.43039700 -1.24557402 -0.60272849 0.66006939 2.05074953 0.49080818
[43] -1.73147942 0.71088366 0.01382291 -1.40104160 1.25912367 -0.12747752
[49] -0.72938651 -1.21136136 0.59961974 -1.16032953 0.43909343 0.20485374
[55] -0.69918134 -0.92662568 -1.01348238 0.60498706 1.73440013 -0.34985053
[61] 1.19918551 1.02390100 -0.04627515 1.38650843 -2.19527272 -0.12755691
[67] -1.31699684 -1.42693514 0.87944163 0.39399158

## Example

head(x1,-20)

## Output

[1] 0.19166338 1.55254427 0.91424229 0.35862537 0.17509564 -0.84726777
[7] 0.97823166 1.80586826 0.12291480 -0.12977203 -0.21642866 1.44647817
[13] 0.40970980 0.91091657 1.43035817 -0.38129196 0.20230718 -0.80619919
[19] 0.29463418 1.40488308 1.02376685 0.47612606 -0.67033033 0.15923432
[25] -0.38271538 0.93576259 -0.63153227 -0.09830608 1.03198498 0.38780843
[31] -1.25612931 -0.78695273 0.42981155 -0.37641622 -1.21622907 1.02927851
[37] 0.43039700 -1.24557402 -0.60272849 0.66006939 2.05074953 0.49080818
[43] -1.73147942 0.71088366 0.01382291 -1.40104160 1.25912367 -0.12747752
[49] -0.72938651 -1.21136136 0.59961974 -1.16032953 0.43909343 0.20485374
[55] -0.69918134 -0.92662568 -1.01348238 0.60498706 1.73440013 -0.34985053
[61] 1.19918551 1.02390100 -0.04627515 1.38650843 -2.19527272 -0.12755691
[67] -1.31699684 -1.42693514 0.87944163 0.39399158 0.68892548 2.35371650
[73] -1.01509767 0.29097752 1.56532608 -0.84711837 1.32766016 0.47704633
[79] 0.18525094 0.25329489

## Example

head(x1,-80)

## Output

[1] 0.1916634 1.5525443 0.9142423 0.3586254 0.1750956 -0.8472678
[7] 0.9782317 1.8058683 0.1229148 -0.1297720 -0.2164287 1.4464782
[13] 0.4097098 0.9109166 1.4303582 -0.3812920 0.2023072 -0.8061992
[19] 0.2946342 1.4048831

## Example

head(x1,-95)

## Output

[1] 0.1916634 1.5525443 0.9142423 0.3586254 0.1750956

## Example

head(x1,-99)


## Output

[1] 0.1916634

## Example

head(x1,-45)

## Output

[1] 0.19166338 1.55254427 0.91424229 0.35862537 0.17509564 -0.84726777
[7] 0.97823166 1.80586826 0.12291480 -0.12977203 -0.21642866 1.44647817
[13] 0.40970980 0.91091657 1.43035817 -0.38129196 0.20230718 -0.80619919
[19] 0.29463418 1.40488308 1.02376685 0.47612606 -0.67033033 0.15923432
[25] -0.38271538 0.93576259 -0.63153227 -0.09830608 1.03198498 0.38780843
[31] -1.25612931 -0.78695273 0.42981155 -0.37641622 -1.21622907 1.02927851
[37] 0.43039700 -1.24557402 -0.60272849 0.66006939 2.05074953 0.49080818
[43] -1.73147942 0.71088366 0.01382291 -1.40104160 1.25912367 -0.12747752
[49] -0.72938651 -1.21136136 0.59961974 -1.16032953 0.43909343 0.20485374
[55] -0.69918134

## Example

x2 <-LETTERS[1:26]
x2

## Output

[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
[20] "T" "U" "V" "W" "X" "Y" "Z"

## Example

head(x2,-6)

## Output

[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
[20] "T"

## Example

head(x2,-20)

## Output

[1] "A" "B" "C" "D" "E" "F"

Updated on: 24-Aug-2020

3K+ Views