Replace Spaces in Column Names in R DataFrame Last Updated : 04 Jan, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will replace spaces in column names of a dataframe in R Programming Language. Let's create a Dataframe with 4 columns with 3 rows: R # create a dataframe with 4 columns and 3 rows data = data.frame("web technologies" = c("php","html","js"), "backend tech" = c("sql","oracle","mongodb"), "middle ware technology" = c("java",".net","python")) # display data Output: In the above example, we can see that there are blank spaces in column names, so we will replace that blank spaces Method 1: Using gsub() Function In this methods we will use gsub function, gsub() function in R Language is used to replace all the matches of a pattern from a string. If the pattern is not found the string will be returned as it is. Syntax: gsub(" ", "replace", colnames(dataframe)) Parameters: first parameter takes spacesecond parameter takes replacing character that replaces blank spacethird parameter takes column names of the dataframe by using colnames() function Example: R program to create a dataframe and replace dataframe columns with different symbols R # create a dataframe with 4 columns and 3 rows data=data.frame("web technologies"=c("php","html","js"), "backend tech"=c("sql","oracle","mongodb"), "middle ware technology"= c("java",".net","python"), check.names=FALSE) # replace blank with underscore print( gsub(" ", "_", colnames(data))) # replace blank with dot operator print( gsub(" ", ".", colnames(data))) # replace blank with * operator print( gsub(" ", "*", colnames(data))) Output: [1] "web_technologies" "backend__tech" "middle_ware_technology" [1] "web.technologies" "backend..tech" "middle.ware.technology" [1] "web*technologies" "backend**tech" "middle*ware*technology" Method 2: Using make.names() function We can do this by using make.names() function. Syntax: make.names(colnames(dataframe)) Where, dataframe is the input dataframe Example: R program to replace dataframe column names using make.names R # create a dataframe with 4 columns and 3 rows data = data.frame("web technologies" = c("php","html","js"), "backend tech" = c("sql","oracle","mongodb"), "middle ware technology" = c("java",".net","python"), check.names = FALSE) # replace blank by using make.names print( make.names(colnames(data))) Output: [1] "web.technologies" "backend..tech" "middle.ware.technology" Comment More infoAdvertise with us Next Article Create DataFrame with Spaces in Column Names in R M manojkumarreddymallidi Follow Improve Article Tags : R Language R Programs R-DataFrame R DataFrame-Programs Similar Reads Create DataFrame with Spaces in Column Names in R In this article, we will see how to create a DataFrame with spaces in column names in R Programming Language. Method 1: Using check.names attribute The data.frame() method in R can be used to create a data frame with individual rows and columns in R. This method contains an attribute check.names, wh 4 min read Replace values from dataframe column using R In this article, we will discuss how to replace values from a DataFrame column in R Programming Language. Whenever we deal with some data, it is necessary that we draw accurate conclusions from it. Now imagine what if, there are some missing values(this happens when some observation is missing in a 3 min read Reorder DataFrame by column name in R It is very difficult any time taking task if we reorder the column name, so we use R Programming Language to do it effectively. In this article, we will be discussing the three different ways to reorder a given DataFrame by column name in R. Method 1: Manually selecting the new order of the column n 2 min read Remove All Whitespace in Each DataFrame Column in R In this article, we will learn how to remove all whitespace in each dataframe column in R programming language. Sample dataframe in use: c1 c2 1 geeks for geeks 2 cs f 3 r -lang gMethod 1: Using gsub() In this approach, we have used apply() function to apply a function to each row in a data frame. T 4 min read Replace Inf with NA in Dataframe in R In this article, we will discuss how to replace Inf (Infinity) with NA in Dataframe in R Programming Language. Create a dataframe with for demonstration: R # create a dataframe with 3 columns and 5 rows data=data.frame(column1 = c(Inf, 2, 3, 4, 5), column2 = c(1, Inf, 1, Inf, 1), column3 = c(1,2,3,I 2 min read Convert Row Names into Column of DataFrame in R In this article, we will discuss how to Convert Row Names into Columns of Dataframe in R Programming Language. Method 1: Using row.names() row.name() function is used to set and get the name of the DataFrame. Apply the row.name() function to the copy of the DataFrame and a name to the column which 3 min read Like