In R, the transform()
function allows you to modify the values of columns in a data frame. You can use it to add new columns, update existing ones, or perform operations on the columns. Here’s a basic structure of how to use the transform()
function:
# Sample data frame
df <- data.frame(
A = c(1, 2, 3),
B = c(4, 5, 6),
C = c(7, 8, 9)
)
# Applying transform to modify columns
df_transformed <- transform(df,
A = A + 10, # Modify column A
B = B * 2, # Modify column B
C = C - 3 # Modify column C
)
# View the modified data frame
print(df_transformed)
In this example:
- Column
A
is increased by 10. - Column
B
is multiplied by 2. - Column
C
is decreased by 3.
Additional Notes:
- The
transform()
function creates a copy of the data frame with the modified values and does not alter the original data frame unless you assign the result back to it (likedf <- transform(...)
). - You can also add new columns during the transformation by simply providing a name for the new column.
Let me know if you need help with any specific transformations!