Learn R -- How to Get Data Frames Columns as Vectors
Join the DZone community and get the full member experience.
Join For Free
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:
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"]
R (programming language)
Data (computing)
Column (database)
Data structure
Frame (networking)
Published at DZone with permission of Ajitesh Kumar, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments