英文:
How to apply a function to specific columns without losing other columns?
问题
我有一个包含日期在第一列和数据在接下来的几列的数据集。我想对数据执行arcsin转换,但不影响日期列。我一直将其作为函数并使用apply,但无法弄清如何创建一个包含转换后数据的新数据框而不丢失日期列。以下是我有的代码:
df <- read.csv("2018data.csv")
fun_asin <- function(x){
return (asin(x/100))
}
df <- data.frame(lapply(df[, 2:4], fun_asin))
这将返回一个包含转换后数据的新数据框,但不包含日期列。你如何更改它以保留未被更改的列?
英文:
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")
答案1
得分: 2
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 generally 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))
英文:
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))
答案2
得分: 0
Two more approaches, one with map_df
and one with nest
and map
:
library(tidyr)
library(purrr)
df %>%
nest(-1) %>%
mutate(fun_asin = map(data, ~fun_asin(.x))) %>%
unnest_wider(fun_asin) %>%
select(-data)
library(purrr)
map_df(df[-1], fun_asin) %>%
cbind(df[1])
英文:
Two more approaches, one with map_df
and one with nest
and map
:
library(tidyr)
library(purrr)
df %>%
nest(-1) %>%
mutate(fun_asin = map(data, ~fun_asin(.x))) %>%
unnest_wider(fun_asin) %>%
select(-data)
library(purrr)
map_df(df[-1], fun_asin) %>%
cbind(df[1])
答案3
得分: 0
使用dplyr
中的across
,这是一种稍微更灵活的方法。
newdf <- df %>%
mutate(across(-where(is.character),
fun_asin))
英文:
Slightly more flexible approach using across
from dplyr
.
newdf <- df %>%
mutate(across(-where(is.character),
fun_asin))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论