How to apply a function to specific columns without losing other columns?

Advertisements

I have a dataset with date in the first column and then my data in the next few columns. I want to perform an arcsin transformation on the data without affecting the date column. I have been doing this as a function and using apply but can’t figure out how to get it to create a new dataframe of the transformed data without losing the date column. This is the code I have:

df <- read.csv("2018data.csv")

fun_asin <- function(x){
return (asin(x/100))}

df <- data.frame(lapply(df[ ,2:4],fun_asin))

This returns a new dataframe with the transformed data but without the date column. How do I change this to keep columns there that aren’t being altered?

Here is the dput for a subset of my dataset:

df <- structure(list(Date.Time = c("9/1/18 12:00", "9/2/18 0:00", "9/2/18 12:00", 
"9/3/18 0:00", "9/3/18 12:00", "9/4/18 0:00", "9/4/18 12:00", 
"9/5/18 0:00", "9/5/18 12:00", "9/6/18 0:00", "9/6/18 12:00", 
"9/7/18 0:00", "9/7/18 12:00", "9/8/18 0:00", "9/8/18 12:00", 
"9/9/18 0:00", "9/9/18 12:00", "9/10/18 0:00", "9/10/18 12:00", 
"9/11/18 0:00"), Narraguagus.R = c(26.38297872, 29.79214781, 
25.06265664, 29.27400468, 29.23433875, 31.89066059, 31.97115385, 
30.71748879, 32.13429257, 27.20930233, 30.21390374, 28.07017544, 
27.68361582, 29.76878613, 31.65680473, 28.61952862, 30.42168675, 
30.37634409, 24.56896552, 24.56140351), Bluehill.R = c(69.48775056, 
73.01401869, 68.46071044, 70.51886792, 73.29545455, 69.72972973, 
68.95459345, 70.28451001, 65.48076923, 63.41929322, 64.20454545, 
66.23246493, 68.88412017, 73.6196319, 75.06112469, 76.06318348, 
76.05839416, 72.01591512, 69.98556999, 69.828722), Jericho.R = c(4.761904762, 
0, 0, 7.692307692, 0, 0, 0, 0, 0, 0, 0, 5.882352941, 0, 0, 0, 
0, 0, 5.882352941, 0, 3.448275862)), row.names = c(NA, 20L), class = "data.frame")

>Solution :

With base R, you just have to manually re-include the column in your output

df_out <- data.frame(df[1], lapply(df[ ,2:4],fun_asin))

You could match that more general with

cols_to_transform <- 2:4
df_out <- data.frame(
  df[setdiff(seq_along(df), cols_to_transform)], 
  lapply(df[cols_to_transform], fun_asin)
)

With dplyr you can use across to apply a function to just a subset of columns

library(dplyr)
df_out <- mutate(df, across(-1, fun_asin))

Leave a ReplyCancel reply