R:如何根据参数中传递的列表中包含的dataFrames创建命名dataframes的函数?
我制作了一个将数据帧作为参数的函数,并根据其中一列的阈值值创建两个数据框。这两个输出数据范围是根据原始输入数据框架命名的。
spliteOverUnder <- function(res){
nm <-deparse(substitute(res))
assign(paste(nm,"_Overexpr", sep=""), res[which(as.numeric(as.character(res$log2FoldChange)) > 1),], pos=1)
assign(paste(nm,"_Underexpr", sep=""), res[which(as.numeric(as.character(res$log2FoldChange)) < -1),], pos=1)
}
该功能正常工作。 我想在此函数上使用循环,以便根据我的标准给出2个数据框,因此我创建了一个包含我的数据范围的列表:
listRes <- list(DJ21_T0, DJ24_T0, DJ29_T0, DJ32_T0,
DJ24_DJ21, DJ29_DJ21, DJ32_DJ21,
DJ21_DJ24, DJ29_DJ24, DJ32_DJ24,
DJ21_DJ29, DJ24_DJ29, DJ32_DJ29,
DJ21_DJ32, DJ24_DJ32, DJ29_DJ32,
Rec2_T0, Rec6_T0, Rec9_T0,
Rec2_DJ32, Rec6_DJ32, Rec9_DJ32,
Rec6_Rec2, Rec9_Rec2,
Rec2_Rec6, Rec9_Rec6,
Rec2_Rec9, Rec6_Rec9)
并使用以下代码:
for (i in 1:length(listRes)){
spliteOverUnder(listRes[[i]])
}
但是,此列表将我返回对象> listres [[i]] _ Overexpr
和listrec [[i]] underexpr
当我这样做循环时,我会遇到相同的问题:
for (i in listRes){
spliteOverUnder(i)
}
它为我提供了对象i_overexpr
和i_underexpr
。
lapply(listers,spliteoverunder)
也不起作用...
如何正确循环我的功能并获取对应于我的数据框的对象? (dj21_t0_overexpr
,dj21_t0_underexpr
,dj24_t0_overexpr
,dj24_t0_underexpr
dj24_t0_underexpr ,... rec6_rec9_underexpr
)
我认为我的功能中使用的技巧deparse(替代(res))
是有问题的,从代码> listres [[i]] ,而不是在我的listres
dataframe列表中列出位置i
的dataframe名称。
欢迎任何帮助。
谢谢
I made a function that takes a dataframe as argument, and creates two dataframes in output according to a threshold value of one of the columns. These 2 output dataframes are named according to the original input dataframe.
spliteOverUnder <- function(res){
nm <-deparse(substitute(res))
assign(paste(nm,"_Overexpr", sep=""), res[which(as.numeric(as.character(res$log2FoldChange)) > 1),], pos=1)
assign(paste(nm,"_Underexpr", sep=""), res[which(as.numeric(as.character(res$log2FoldChange)) < -1),], pos=1)
}
The function works correctly.
I would like to use a loop on this function so that each of my dataframes gives 2 dataframes according to my criteria, so I created a list that contains my dataframes:
listRes <- list(DJ21_T0, DJ24_T0, DJ29_T0, DJ32_T0,
DJ24_DJ21, DJ29_DJ21, DJ32_DJ21,
DJ21_DJ24, DJ29_DJ24, DJ32_DJ24,
DJ21_DJ29, DJ24_DJ29, DJ32_DJ29,
DJ21_DJ32, DJ24_DJ32, DJ29_DJ32,
Rec2_T0, Rec6_T0, Rec9_T0,
Rec2_DJ32, Rec6_DJ32, Rec9_DJ32,
Rec6_Rec2, Rec9_Rec2,
Rec2_Rec6, Rec9_Rec6,
Rec2_Rec9, Rec6_Rec9)
and used the following code:
for (i in 1:length(listRes)){
spliteOverUnder(listRes[[i]])
}
But this one returns me the objects listRes[[i]]_Overexpr
and listRec[[i]]Underexpr
I encounter the same problem when I do the loop like this:
for (i in listRes){
spliteOverUnder(i)
}
Which gives me the objects i_Overexpr
and i_Underexpr
.
lapply(listRes, spliteOverUnder)
doesn't work either...
How to loop correctly my function and get the objects corresponding to my dataframes ? (DJ21_T0_Overexpr
, DJ21_T0_Underexpr
, DJ24_T0_Overexpr
, DJ24_T0_Underexpr
, ... , Rec6_Rec9_Overexpr
, Rec6_Rec9_Underexpr
)
I think the trick deparse(substitute(res))
used in my function is problematic, giving the created objects the name i
or listRes[[i]]
rather than giving the name of the dataframe at position i
in my listRes
dataframe list.
Any help is welcome.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个整理解决方案,它可以避免使用
MAP
而明确编写循环。请注意,首先,您可能可以使用分组或嵌套的数据帧来完成整个操作,从而避免需要创建对象。但是,如果您确实要创建对象(或者也许启动DF具有不同数量的列,即使它们都具有log2FoldChange
列),那么您可以执行以下操作。首先,一些设置使示例可重现。
接下来,获取这五个数据框架并将其放入列表中,这是问题开始的地方。
现在,使用此数据框架列表,我们可以将每个列表分为上/下。如果拆分标准更为复杂,我们可以编写一个函数来执行此操作。但是在这里,我们使用
if_else
根据cyl
的阈值在每个数据框架中创建一个新列。现在我们有了一个嵌套列表。
df_1
todf_5
的每一个都被分为上下表。我们可以通过这很方便地查看它们,因为我们可以使用IDE中的选项卡完成来调查列表中的表。
我们可以与这样的列表合作。或者,假设它们都具有相同的列,则可以将它们绑在一个大的DF中。但是,OP希望它们作为带有后缀
_OVER
或_UNDER
的单独数据框架OBJEC。因此,例如,要提取所有“ over” dfs,并使它们具有名称df_1_over等的对象,我们现在可以在我们的环境中进行操作,例如,
我们可以以相同的方式将DFS作为对象。
同样,根据所需的内容,从开始到完成整个事情可能会更有意义,并根据需要将数据分组。或者,如果我们知道原始DF都具有相同的列布局,请通过行将它们绑定到由其名称索引的DF,如下:
从那里,您可以通过
id> id
将大df分组/根据措施等Here's a tidyverse solution that avoids the need to explicitly write loops, using
map
instead. Note at the outset that you could probably do the whole thing using grouped or nested data frames, thus avoiding the need to create the objects. But if you do want to create the objects (or perhaps the starting dfs have different numbers of columns, even if they all have thelog2FoldChange
column) then you could do something like the following.First, some setup to make the example reproducible.
Next, get these five data frames and put them in a list, which is where the question starts from.
Now, working with this list of data frame, we can split each one into the over/under. If the split criteria were more complex we could write a function to do it. But here we use
if_else
to create a new column in each data frame based on a threshold value ofcyl
.Now we have a nested list. Each of
df_1
todf_5
is split into an over or under table. We can look at them by e.g.This is handy because we can use tab completion in our IDE to investigate the tables in the list.
We could just work with the list like this. Or we could bind them into a big df, by row, assuming they all have the same columns. But the OP wanted them as separate data frame objecs with a suffix
_over
or_under
. So, e.g. to extract all the "over" dfs and make them objects with names df_1_over etc, we can doNow in our environment we have e.g.
We can get the "under" dfs as objects in the same way.
Again, depending on what was needed it might make more sense to do the whole thing from start to finish using a single tibble and grouping the data as needed. Or, if we know the original dfs all have the same columnar layout, bind them by row into a df indexed by their name, like this:
From there you can group the big df by
id
make the over/under measures etc.最后,主要的问题是将对象及其名称之间的区别区分,而不是忘记创建数据范围列表会删除这些数据范围的名称。因此,使用
name()
函数非常有用。请小心以与列表中包含的顺序相同的顺序命名对象。Finally, the main problem was to make the distinction between the object and its name, and not to forget that creating a list of dataframes erases the name of these dataframes. The use of the
names()
function is therefore very useful. Be careful to name the objects in the same order as they are contained in the list.