R df space in column name

To remove the white space in a column name in R, you can follow the steps below:

  1. Create a DataFrame: First, create a DataFrame with a column that has white space in its name. For example, let's create a DataFrame called df with a column named "Column Name " (with a space at the end):

R df <- data.frame("Column Name " = c(1, 2, 3))

  1. Rename the column: Use the colnames() function to access and modify the column names of the DataFrame. In this case, we will use the gsub() function to substitute the white space with an empty string. Assign the modified column names back to the DataFrame.

R colnames(df) <- gsub(" ", "", colnames(df))

The gsub() function is used to replace all occurrences of a pattern with a replacement string. In this case, we are replacing the white space with an empty string.

  1. Verify the changes: Check the column names of the DataFrame using the colnames() function again to ensure that the white space has been removed.

R colnames(df)

The output should show that the white space has been removed from the column name:

[1] "ColumnName"

By following these steps, you should be able to remove white space from a column name in R.