How to Loop Through Column Names in R dataframes? Last Updated : 12 Dec, 2022 Comments Improve Suggest changes Like Article Like Report In this article, we will discuss how to loop through column names in dataframe in R Programming Language. Method 1: Using sapply() Here we are using sapply() function with some functions to get column names. This function will return column names with some results Syntax: sapply(dataframe,specific function) where dataframe is the input dataframespecific function is like mean,sum,min ,max etc Example: R program to get column names in the dataframe by performing some operations R # create dataframe with 4 columns data = data.frame(column1=c(23, 45), column2=c(50, 39), column3=c(33, 45), column4=c(11, 39)) # display print(data) # get mean of all columns print(sapply(data, mean)) # get sum of all columns print(sapply(data, sum)) # get minimum of all columns print(sapply(data, min)) # get maximum of all columns print(sapply(data, max)) Output: column1 column2 column3 column4 1 23 50 33 11 2 45 39 45 39 column1 column2 column3 column4 34.0 44.5 39.0 25.0 column1 column2 column3 column4 68 89 78 50 column1 column2 column3 column4 23 39 33 11 column1 column2 column3 column4 45 50 45 39 Method 2: Using colnames By using this function we can get column names. We have to iterate through for loop to get all the column names. Syntax: for (iterator in colnames(dataframe)){ print(iterator ) } where dataframe is the input dataframeiterator is a variable is used to iterate the elements Example: R # create dataframe with 4 columns data = data.frame(column1=c(23, 45), column2=c(50, 39), column3=c(33, 45), column4=c(11, 39)) # display print(data) # display column names for (i in colnames(data)){ print(i) } Output: Comment More infoAdvertise with us Next Article How to Loop Through Column Names in R dataframes? G gottumukkala_sivanagulu Follow Improve Article Tags : R Language R-DataFrame R DataFrame-Programs Similar Reads How to add multiple columns to a data.frame in R? In R Language adding multiple columns to a data.frame can be done in several ways. Below, we will explore different methods to accomplish this, using some practical examples. We will use the base R approach, as well as the dplyr package from the tidyverse collection of packages.Understanding Data Fr 4 min read How to plot all the columns of a dataframe in R ? In this article, we will learn how to plot all columns of the DataFrame in R programming language. Dataset in use: x y1 y2 y3 1 1 0.08475635 0.4543649 0 2 2 0.22646034 0.6492529 1 3 3 0.43255650 0.1537271 0 4 4 0.55806524 0.6492887 3 5 5 0.05975527 0.3832137 1 6 6 0.08475635 0.4543649 0 7 7 0.226460 5 min read How to get name of dataframe column in PySpark ? In this article, we will discuss how to get the name of the Dataframe column in PySpark. To get the name of the columns present in the Dataframe we are using the columns function through this function we will get the list of all the column names present in the Dataframe. Syntax: df.columns We can a 3 min read How to Convert a List to a Dataframe in R We have a list of values and if we want to Convert a List to a Dataframe within it, we can use a as.data.frame. it Convert a List to a Dataframe for each value. A DataFrame is a two-dimensional tabular data structure that can store different types of data. Various functions and packages, such as dat 4 min read Convert Data Frame Column to Numeric in R In R, converting DataFrame columns to numeric is a common and important step in data preprocessing, particularly for statistical modeling and analysis. Below are effective methods to perform this conversion using base R and the readr package.Method 1: Convert One Column to NumericStart by checking t 2 min read Like