Create a data frame and write a function to add a new column to it.
MediumProblem Statement
Create a data frame and write a function to add a new column to it.
Sample Input
Data Frame: Name Age 1 John 25 2 Alice 30 3 Bob 22 New Column: Salary = c(50000, 60000, 45000)
Sample Output
Name Age Salary 1 John 25 50000 2 Alice 30 60000 3 Bob 22 45000
Limits
Time: 2s · Memory: 256 MB
Code Editor
00:00
# Predefined structure
data <- # Create the data frame
add_column <- function(df, new_col_name, new_col_values) {
df[[new_col_name]] <- new_col_values
return(df)
}
updated_data <- add_column(data, "Salary", c(50000, 60000, 45000))
print(updated_data)
Input
Data Frame: Name Age 1 John 25 2 Alice 30 3 Bob 22 New Column: Salary = c(50000, 60000, 45000)
Expected Output
Name Age Salary 1 John 25 50000 2 Alice 30 60000 3 Bob 22 45000