对列表中的所有数据框执行相同的操作
我有一个数据框列表,我需要对所有数据框执行相同的操作。我不确定是否最好创建一个函数或循环,然后将其应用于列表中的所有内容。我都尝试过,但我无法让任何东西发挥作用。我已经尝试过:
prepare_df <- function(df) {
# make changes to df
names(df)[1] <- 'row'
df <- df %>%
pivot_longer((!row), names_to = "plateColumn", values_to = "Broth_t0")
df <- df %>%
mutate(wellID = paste0(row, plateColumn)) %>%
select(-c(row, plateColumn))
return(df)
并且我已经尝试过:
change <- function(i){
i <- i %>%
pivot_longer((!row), names_to = "plateColumn", values_to = "Treatment") %>%
mutate(get(wellID = paste0(row, plateColumn))) %>%
select(-c(row, plateColumn))
}
我想知道是否更容易一步一步地完成我需要做的事情,而不是尝试将所有内容合并到 1 个函数/循环中,但我需要能够完成所有这些工作以下。 mr1_44_312_t0 是单独的数据帧,我只是复制和粘贴,但我有太多数据,无法继续使用该方法。我对 R 相当陌生,所以任何帮助将不胜感激!
mr1_44_312_t0 <- mr1_44_312_t0[36:43, 2:14]
colnames(mr1_44_312_t0) [2:13] <- (1:12)
names(mr1_44_312_t0)[1] <- 'row'
mr1_44_312_t0 <- mr1_44_312_t0 %>%
pivot_longer((!row), names_to = "plateColumn", values_to = "Absorbance_t0")
mr1_44_312_t0 <- mr1_44_312_t0 %>%
mutate(wellID = paste0(row, plateColumn)) %>%
select(-c(row, plateColumn))
I have a list of data frames and I need to do the same thing to all of them. I'm not sure if it would be best to create a function, or a loop and then apply it to everything in the list. I have tried both and I cannot get anything to work. I have tried:
prepare_df <- function(df) {
# make changes to df
names(df)[1] <- 'row'
df <- df %>%
pivot_longer((!row), names_to = "plateColumn", values_to = "Broth_t0")
df <- df %>%
mutate(wellID = paste0(row, plateColumn)) %>%
select(-c(row, plateColumn))
return(df)
and I have tried:
change <- function(i){
i <- i %>%
pivot_longer((!row), names_to = "plateColumn", values_to = "Treatment") %>%
mutate(get(wellID = paste0(row, plateColumn))) %>%
select(-c(row, plateColumn))
}
I'm wondering if it is easier to do what I need to do step by step instead of trying to incorporate everything into 1 function/loop but I need to be able to do all of that is below. mr1_44_312_t0 is the individual dataframe and I have been just copying and pasting, but I have too much data to continue with that method. I am fairly new to R, so any help would be appreciated!
mr1_44_312_t0 <- mr1_44_312_t0[36:43, 2:14]
colnames(mr1_44_312_t0) [2:13] <- (1:12)
names(mr1_44_312_t0)[1] <- 'row'
mr1_44_312_t0 <- mr1_44_312_t0 %>%
pivot_longer((!row), names_to = "plateColumn", values_to = "Absorbance_t0")
mr1_44_312_t0 <- mr1_44_312_t0 %>%
mutate(wellID = paste0(row, plateColumn)) %>%
select(-c(row, plateColumn))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们可以使用
map
来循环list
We may use
map
to loop over thelist