Learn R – How to Get Data Frames Columns as Vectors

This article represents different ways in which one could get a data frame column as a vector. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos.
4 Techniques to Get Data Frame Column as Vector

In the examples below, diamonds dataset from ggplot2 package is considered. This is how a diamond dataset looks like:

head_diamonds

Following are four different technique/method using which one could retrieve a data frame column as a vector.

# In the data set shown above, carat represents column name and hence, [['carat']]
carat1 <- diamonds[['carat']]
# In the data set shown above, carat represents 1st column and hence, index 1
carat2 <- diamonds[,1]
# Pay attention to usage of dollar $ operator
carat3 <- diamonds$carat
# In the data set shown above, carat represents column name and hence, [, "carat"]
carat4 <- diamonds[,"carat"]
Ajitesh Kumar

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.
Posted in Big Data. Tagged with .