How to Convert an Excel Column into a List of vectors in R?

In this article, we will look at various methods for converting Excel columns to vectors in the R programming language

Let us use the below given sampleExcelFile.xlsx Excel File here:

sample-excel-file-sheet1-image

 

Converting an Excel Column into a List of vectors in R

Below are the ways to convert an Excel Column into a list of vectors:

Method #1: Using $-Operator (with the column name)

Here we will just use the $-operator with the column name and the name of the data retrieved from the excel file in this method.

  • The name of the column to be converted in the vector is written at the end of the $-operator, while the name of the data read from the excel file is written before the $-operator.

Syntax:

dataframe$column

Approach:

  • Import readxl package using the library() function.
  • Pass the excel file path as an argument to the read_excel() function to read the given excel file and store it in a variable.
  • Get the column data as a vector using the $ operator by passing the column name and applying it to the above excel file.
  • Display the above vector.
  • The Exit of the Program.

Below is the implementation:

# Import readxl package using the library() function
library(readxl)

# Pass the excel file path as an argument to the read_excel() function and 
# to read the given excel file and store it in a variable
gvn_exceldata<- read_excel("C:/Users/cirus/Downloads/excelFile.xlsx")
# Get the column data as vector using $ operator by passing the column name and apply it on the above excel file
rslr_vector<-gvn_exceldata$age                       
# Display the above vector 
rslr_vector

Output:

 [1] 21 22 19 20 18 23 32 34 36 39 42 66

Method #2: Using the Method of Subsetting Column

In this method, the user only needs to enter the column name inside the brackets columns with the data. To turn that Excel column into a vector. This will just convert the column to the vector that the user passed with the brackets along with its data.

Syntax:

dataframe[row_name,column_name]

Approach:

  • Import readxl package using the library() function.
  • Pass the excel file path as an argument to the read_excel() function to read the given excel file and store it in a variable.
  • Get the column data as a vector by passing the column name in the [] operator and apply it to the above excel file.
  • Display the above vector.
  • The Exit of the Program.

Below is the implementation:

# Import readxl package using the library() function
library(readxl)

# Pass the excel file path as an argument to the read_excel() function and 
# to read the given excel file and store it in a variable
gvn_exceldata<- read_excel("C:/Users/cirus/Downloads/excelFile.xlsx")
# Get the column data as vector by passing the column name in [] operator and apply it to the above excel file 
rslr_vector<- gvn_exceldata[ , "Website name"]                       
# Display the above vector 
rslr_vector

Output:

# A tibble: 12 × 1
`Website name` 
<chr> 
1 Sheets Tips 
2 Python-Programs
3 BTechGeeks 
4 PythonArray 
5 Sheets Tips 
6 Python-Programs
7 Sheets Tips 
8 Python-Programs
9 BTechGeeks 
10 PythonArray 
11 Sheets Tips 
12 Python-Programs

Method #3: Using the pull function from dplyr package

In this method, the user must invoke the pull() function from the R language’s dplyr package, passing the column name to be converted into vector form and the name of the datafile read variable.

Installation:

install.packages("dplyr")

Output:

Installing package into ‘C:/Users/cirus/AppData/Local/R/win-library/4.2’
(as ‘lib’ is unspecified)
--- Please select a CRAN mirror for use in this session ---
trying URL 'https://cloud.r-project.org/bin/windows/contrib/4.2/dplyr_1.0.9.zip'
Content type 'application/zip' length 1290875 bytes (1.2 MB)
downloaded 1.2 MB

package ‘dplyr’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
C:\Users\cirus\AppData\Local\Temp\RtmpE3dtfs\downloaded_packages

pull Function:

The pull function selects a column from a data frame and converts it to a vector.

Syntax:

pull(.data, j)

Parameters

  • .data: It is the name of the data.
  • j: It is the column that must be extracted.

Return Value: A vector of the specified column is returned by the pull() function .

Approach:

  • Import readxl package using the library() function.
  • Pass the excel file path as an argument to the read_excel() function to read the given excel file and store it in a variable.
  • Import the dplyr package using the library() function
  • Pass the given excel file and column name to the pull function.
  • Display the above vector.
  • The Exit of the Program.

Below is the implementation:

# Import readxl package using the library() function
library(readxl)

# Pass the excel file path as an argument to the read_excel() function and 
# to read the given excel file and store it in a variable
gvn_exceldata<- read_excel("C:/Users/cirus/Downloads/excelFile.xlsx")
# Import the dplyr package using the library() function
library("dplyr")                        
# Pass the given excel file and column name to the pull function
rslr_vector <- pull(gvn_exceldata,age)  
# Display the above vector 
rslr_vector

Output:

 [1] 21 22 19 20 18 23 32 34 36 39 42 66

 

Leave a Comment