How to remove,append and sort the vector

 

Method 1: Remove elements using in operator

This operator will select specific elements and uses ! operator to exclude those elements.

Syntax:

vector[! vector %in% c(elements)]

Example:

In this example, we will be using the in operator to remove the elements in the given vector in the R programming language.

# create a vector

vector1=c(1,34,56,2,45,67,89,22,21,38)

  

# display 

print(vector1)

  

# remove the elements

print(vector1[! vector1%in% c(34,45,67)])

 

Note: We can also specify the range of elements to be removed.

Syntax:

vector[! vector %in% c(start:stop)]

Example:

# create a vector

vector1 = c(1, 34, 56, 2, 45, 67, 89, 22, 21, 38)

  

# display

print(vector1)

  

# remove the elements

print(vector1[! vector1 % in % c(1:50)])

 

Method 2: Using conditions to remove the elements

In this method, the user needs to use the specific conditions accordingly to remove the elements from a given vector.

Syntax:

vector[!condition]

where

·         condition: specifies the condition that element be checked

# create a vector

vector1=c(1,34,56,2,45,67,89,22,21,38)

  

# display 

print(vector1)

  

# remove the elements with element greater 

# than 50 or less than 20

print(vector1[!(vector1 > 50 | vector1 < 20)])

 

 

Appending values to an empty vector

Here we are going to append the values using for loop to the empty vector.

Syntax:

for(iterator in range) {
  vector = c(vector, iterator)
}

where,

·         range is the range of values

·         iterator is to iterate the range of values

·         c(vector,iterator) is an append function which will append values to the vector

Example:

 

# create empty vector

vector1 = c()

  

# display

print(vector1)

  

# use for loop to add elements from 1 to 5

for(i in 1: 5) {

    vector1 = c(vector1, i)

}

  

# display

vector1

 

Perform Operation and Append Values to Vector

Here we are going to perform some numeric operations and append values to the empty vector. We can perform cube operation and append to empty vector.

Syntax:

for(iterator in range) {
 vector = c(vector, operation(iterator))
}

where,

·         range is the range of values

·         iterator is to iterate the range of values

·         c(vector,operation(iterator) ) is an append function which will append values to the vector by performing some operation

Here we are going to append the cube values to the vector

Example:

 

# create empty vector

vector1 = c()

  

# display

print(vector1)

  

# use for loop to add elements from 

# 1 to 5 with cube values

for(i in 1: 5) {

    vector1 = c(vector1, i*i*i)

}

  

# display

vector1

 

Append a Single Value to Vector

Here we are going to append a value for an existing vector.

Syntax:

c(existing_vector,new)

where,

·         existing_vector is the vector

·         new is the values to be appended

Example:

# create  vector

vector1 = c(1, 2, 3, 4, 5)

  

# display

print(vector1)

  

# append 34

vector1 = c(vector1, 34)

  

# display

vector1

 

 

Append Multiple Values to Existing Vector

Here we are going to append multiple values to the existing vector using for loop.

Syntax:

for(iterator in range) {
 vector = c(existing_vector, iterator)
}

where,

·         range is the range of values

·         iterator is to iterate the range of values

·         c(existing_vector,iterator) is an append function which will append values to the existing  vector

# create  vector

vector1 = c(6, 7, 8, 9, 10)

  

# display

print(vector1)

  

# use for loop to add elements from 1 to 5

for(i in 1: 5) {

    vector1 = c(vector1, i)

}

  

# display

vector1

 

 

Sorting vector Alphabetically

Here we are using sort() function to sort a vector alphabetically.

Syntax:

sort(vector)

where, vector is the input vector

Example:

# create a vector

vector1 = c('G', 'E', 'E', 'K', 'S')

  

# sort the vector

print(sort(vector1))

 

Sorting Data Frame Column Alphabetically

We can create a dataframe by using date.frame() function. We can sort a dataframe column by using order() function

Syntax:

dataframe[order(dataframe$column_name),]

where,

·         dataframe is the input dataframe

·         column_name is the column that includes alphabetical values based on this column

Example:

# create a dataframe with 3 columns

data = data.frame(name1=c('G', 'E', 'E', 'K', 'S'),

                  name2=c('P', 'Y', 'T', 'H', 'O'), 

                  marks=c(78, 89, 77, 89, 78))

  

# sort the dataframe based on name1 column

print(data[order(data$name1), ])

  

# sort the dataframe based on name2 column

print(data[order(data$name2), ])

 

Method 3: Sort Multiple Columns Alphabetically

We can also sort multiple columns in dataframe by using order function.

Syntax:

dataframe[with(dataframe, order(column1, column2,.,column n)), ]

Example:

# create a dataframe with 3 columns

data = data.frame(name1=c('G', 'E', 'E', 'K', 'S'), 

                  name2=c('P', 'Y', 'T', 'H', 'O'), 

                  marks=c(78, 89, 77, 89, 78))

  

# sort the dataframe based on name1 and 

# name2 columns

print(data[with(data, order(name1, name2)), ])

 

Comments

Popular posts from this blog

How to create Animated 3d chart with R.

Linux/Unix Commands frequently used

R Programming Introduction