处理R中的重复任务 [英] Dealing with repetitive tasks in R

查看:157
本文介绍了处理R中的重复任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常发现自己不得不在R中执行重复的任务。不得不一次又一次地在一个或多个数据结构上持续运行相同的功能非常令人沮丧。



例如,假设我在R中有三个单独的数据帧,我想删除每个具有缺失值的数据帧中的行。使用三个数据帧,在每个df上运行na.omit()并不是很困难,但是当具有一百个需要相同操作的类似数据结构时,它可以获得非常低效的

  df1 < -  data.frame(Region = c(Asia,Africa,Europe,N.America ,S.America,NA),
variable = c(2004,2004,2004,2004,2004,2004),value = c(35,20,20,50,30,NA))

df2< - data.frame(Region = c(亚洲,非洲,欧洲,美国,美国印第安人,NA),
可变= c(2005,2005,2005,2005,2005,2005),value = c(55,350,40,90,99,NA))

df3< - data.frame(Region = c (亚洲,非洲,欧洲,美国,美国印第安人,NA),
variable = c(2006,2006,2006,2006,2006,2006) = c(300,200,200,500,300,NA))

tot04< - na.omit(df1)
tot05< - na.omit(df2)
tot06< - na。省略(df3)

在R中处理重复任务的一些一般准则是什么?



是的,我认识到这个问题的答案是特定于一个人面临的任务,但我只是问一个用户在重复任务时应该考虑的一般事情。

解决方案

作为一般准则,如果您有几个对象要应用相同的操作,则应将其收集到一个数据结构。然后可以使用循环,[sl]应用等一次操作。在这种情况下,您可以将它们放在一个列表中,而不是单独的数据框架 df1 df2 数据框,然后运行 na.omit

  dflist<  -  list(df1,df2,&...)
dflist< - lapply(dflist,na.omit)


I often find myself having to perform repetitive tasks in R. It gets extremely frustrating having to constantly run the same function on one or more data structures over and over again.

For example, let's say I have three separate data frames in R, and I want to delete the rows in each data frame which possess a missing value. With three data frames, it's not all that difficult to run na.omit() on each of the df's, but it can get extremely inefficient when one has one hundred similar data structures which require the same action.

df1 <- data.frame(Region=c("Asia","Africa","Europe","N.America","S.America",NA),
             variable=c(2004,2004,2004,2004,2004,2004), value=c(35,20,20,50,30,NA))

df2 <- data.frame(Region=c("Asia","Africa","Europe","N.America","S.America",NA),
            variable=c(2005,2005,2005,2005,2005,2005), value=c(55,350,40,90,99,NA))

df3 <- data.frame(Region=c("Asia","Africa","Europe","N.America","S.America",NA),
           variable=c(2006,2006,2006,2006,2006,2006), value=c(300,200,200,500,300,NA))

tot04 <- na.omit(df1)
tot05 <- na.omit(df2)
tot06 <- na.omit(df3)

What are some general guidelines for dealing with repetitive tasks in R?

Yes, I recognise that the answer to this question is specific to the task that one faces, but I'm just asking about general things that a user should consider when they have a repetitive task.

解决方案

As a general guideline, if you have several objects that you want to apply the same operations to, you should collect them into one data structure. Then you can use loops, [sl]apply, etc to do the operations in one go. In this case, instead of having separate data frames df1, df2, etc, you could put them into a list of data frames and then run na.omit on all of them:

dflist <- list(df1, df2, <...>)
dflist <- lapply(dflist, na.omit)

这篇关于处理R中的重复任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆