简体   繁体   English

使用If从R中的列表中按名称选择数据框

[英]Using If to select dataframe by name from list in R

In R: I have a list of 3 dataframes (Book1, Book2, Book3), list named dflist4. 在R中:我有3个数据帧的列表(Book1,Book2,Book3),列表名为dflist4。 I have a code I want to apply to each dataframe separately in the list because the value of maxm is different for each dataframe. 我有一个代码想要分别应用于列表中的每个数据框,因为maxm的值对于每个数据框都不同。 I wrote it, and it works, but only when Book1, Book2, and Book3 are all equally sized dataframes. 我编写了它,并且它起作用了,但是仅当Book1,Book2和Book3的大小均相等时才是如此。 When they are not equally sized, the code will not run (error: ops.dataframe == only defined for equally-sized dataframes). 当它们的大小不相等时,代码将不会运行(错误:ops.dataframe ==仅为大小相等的数据帧定义)。 When I change the == to = , i get that it is not logical. 当我将==更改为=时,我发现这是不合逻辑的。 Can anyone please give a suggestion as to how to select the dataframes from the list based on their names no matter the size of the dataframe? 任何人都可以提出建议,无论数据帧的大小如何,如何根据其名称从列表中选择数据帧?

Code here: 代码在这里:

eggplant<-function(x){
(if((x == (dflist4[["Book1"]])){
maxm = 3;
x %>% mutate(Col4 = (x[,3])/maxm);
})
(if((x == dflist4[["Book2"]])){
maxm = 2;
x %>% mutate(Col4 = (x[,3])/maxm);
})
(if((x == dflist4[["Book3"]])){
maxm = 1;
x %>% mutate(Col4 = (x[,3])/maxm);
})
}

test<-lapply(dflist4, eggplant)

Following up from my comments above, I assume the third column in Book1 , Book2 , Book3 is called Col3 . 根据上面的评论,我假设Book1Book2Book3的第三列称为Col3

You can use purrr::map2 您可以使用purrr::map2

library(tidyverse)
purrr::map2(dflist4, c(3, 2, 1), function(df, maxm) df %>% mutate(Col4 = Col3 / maxm))

As you don't provide sample data, here is an mtcars -based example 由于您不提供示例数据,因此以下是基于mtcars的示例

purrr::map2(list(mtcars[1:3, ], mtcars[1:3, ]), c(10, 100), function(df, maxm)
    df %>% mutate(mpg.new = mpg / maxm))
#[[1]]
#   mpg cyl disp  hp drat    wt  qsec vs am gear carb mpg.new
#1 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4    2.10
#2 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4    2.10
#3 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1    2.28
#
#[[2]]
#   mpg cyl disp  hp drat    wt  qsec vs am gear carb mpg.new
#1 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4   0.210
#2 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4   0.210
#3 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1   0.228

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R:从数据帧列表中的 dataframe 名称设置列名称 - R: Set column name from dataframe name in list of dataframes 使用 R 中的列表和列表名称的值替换 dataframe 列的值 - replacing values of a dataframe column using values of a list and list name in R 如何使用R中的循环在列表内传递数据框列表的名称 - how to pass the name of dataframe list inside a list using a loop in r 如何使用 R 中的列名从另一个列表中获取 select 表列表? - How to select a list of tables from another list using a column name in R? 当从列表中调用数据帧时,使用 dataframe 的名称作为 R 中 function 中的图形标题 - Using name of dataframe as graph title in a function in R when dataframes are being called from a list 使用 dplyr Z99938282F0416EF8599ZFCF2E8 访问 R 中的 dataframe 中的列表对象 - Accessing list objects in a dataframe in R using dplyr select 将数据框列表中的数据框用作R中的函数参数,并通过其名称进行调用 - Using a dataframe in a list of dataframes as a function argument in R and calling it by its name 如何从相当于 R 的 Python 数据帧列表中选择特定数据帧? - How to select a particular dataframe from a list of dataframes in Python equivalent to R? R - 从包含其名称的列表中调用数据框 - R - Call a dataframe from a list which contains its name 使用R中的列表从for循环输出名称 - Name output from a for loop using a list in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM