Append Characters in Front of Numeric Data Frame Variable in R (Example Code)
In this R tutorial you’ll learn how to append characters at the beginning of a numeric data frame variable.
Introduction of Example Data
data(iris) # Loading an exemplifying data frame head(iris) # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 3 4.7 3.2 1.3 0.2 setosa # 4 4.6 3.1 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa # 6 5.4 3.9 1.7 0.4 setosa |
data(iris) # Loading an exemplifying data frame head(iris) # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 3 4.7 3.2 1.3 0.2 setosa # 4 4.6 3.1 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa # 6 5.4 3.9 1.7 0.4 setosa
Example: Adding Characters to Numeric Column Using the paste0() Function
iris_upd <- iris # Creating new data frame with characters iris_upd$Sepal.Length <- paste0("char", iris_upd$Sepal.Length) head(iris_upd) # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 char5.1 3.5 1.4 0.2 setosa # 2 char4.9 3.0 1.4 0.2 setosa # 3 char4.7 3.2 1.3 0.2 setosa # 4 char4.6 3.1 1.5 0.2 setosa # 5 char5 3.6 1.4 0.2 setosa # 6 char5.4 3.9 1.7 0.4 setosa |
iris_upd <- iris # Creating new data frame with characters iris_upd$Sepal.Length <- paste0("char", iris_upd$Sepal.Length) head(iris_upd) # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 char5.1 3.5 1.4 0.2 setosa # 2 char4.9 3.0 1.4 0.2 setosa # 3 char4.7 3.2 1.3 0.2 setosa # 4 char4.6 3.1 1.5 0.2 setosa # 5 char5 3.6 1.4 0.2 setosa # 6 char5.4 3.9 1.7 0.4 setosa
Further Resources & Related Tutorials
Below, you can find some further resources that are similar to the content of this page.