如何使用大量代码在R中创建函数?

时间:2018-06-28 12:59:08

标签: r function

我认为这是一个相当基本的问题,因为我是R的新用户,但是我想这样做,以便我可以用单个条目/单词激活下面的整个代码(我想这将是一个函数) 。如果已经提出了此要求,我深表歉意,请转给我答复的链接。预先感谢您的所有帮助。 我的代码:

head(yelp, 10)
str(yelp)
yelp_flat<- flatten(yelp)
str(yelp_flat)
library(tibble)
yelp_tbl <- as_data_frame(yelp_flat)
yelp_tbl
yelp_tbl$newcolumn <- NULL
yelp_tbl$newcolumn1 <- NULL
yelp_tbl$shotClock <- NULL
yelp_tbl$period <- NULL
yelp_tbl$wallClock <- NULL
yelp_tbl$gameClock <- NULL
yelp_tbl$gameClockStopped <- NULL
yelp_tbl$ball <- NULL
head(yelp_tbl)
good <- unnest(yelp_tbl) #extracts xyz from original dataframe
library(tidyr)
player <- good %>% separate(xyz, c("player_x", "player_y", "player_z"), sep = ",")
finish <- player %>% separate(xyz1, c("player_x", "player_y", "player_z"), sep = ",")
k <- finish %>% separate(player_x, c("trash", "player_x"), sep = "c")
k$trash <- NULL
r <- k %>% separate(player_z, c("player_z", "tra"), sep = "\\)")
u <- r %>% separate(player_x, c("kol", "player_x"), sep = "\\(")
Away_Team <- u
Away_Team$garbage <- NULL
Away_Team$playerId1<- NULL
Away_Team$aplayer_x <- NULL
Away_Team$aplayer_y <- NULL
Away_Team$aplayer_z <- NULL
Away_Team$dispose <- NULL
Away_Team$brack <- NULL
Away_Team$kol <- NULL
Away_Team$tra <- NULL
View(Away_Team)
yelp_tbl
yelp_tbl$newcolumn <- NULL
yelp_tbl$newcolumn1 <- NULL
yelp_tbl$shotClock <- NULL
yelp_tbl$period <- NULL
yelp_tbl$wallClock <- NULL
yelp_tbl$gameClock <- NULL
yelp_tbl$gameClockStopped <- NULL
yelp_tbl$ball <- NULL
head(yelp_tbl)
good <- unnest(yelp_tbl) #extracts xyz from original dataframe
library(tidyr)
player <- good %>% separate(xyz, c("player_x", "player_y", "player_z"), sep = ",")
finish <- player %>% separate(xyz1, c("player_x", "player_y", "player_z"), sep = ",")
k <- finish %>% separate(player_x, c("trash", "player_x"), sep = "c")
k$trash <- NULL
r <- k %>% separate(player_z, c("player_z", "tra"), sep = "\\)")
u <- r %>% separate(player_x, c("kol", "player_x"), sep = "\\(")
Home_Team <- u
Home_Team$garbage <- NULL
Home_Team$playerId1<- NULL
Home_Team$hplayer_x <- NULL
Home_Team$hplayer_y <- NULL
Home_Team$hplayer_z <- NULL
Home_Team$dispose <- NULL
Home_Team$brack <- NULL
Home_Team$kol <- NULL
Home_Team$tra <- NULL
View(Home_Team)
View (Away_Team)
Table <- rbind(Home_Team, Away_Team)
View(Table) #order frameIdx to see correct order

2 个答案:

答案 0 :(得分:2)

因此,确实应该执行一个功能。请按照以下步骤操作:

1。将所有代码放入函数中

my_function <- function(){
   # Your code
}

2。识别输入的内容(也就是您未在代码中构建的内容),它们将成为函数的参数

my_function <- function(arg1, arg2, ...){
   # Your code
}

在您的示例中,我确定了yelp

3。确定要输出的内容(最好是一个对象),它们将位于函数的return

my_function <- function(arg1, arg2, ...){
  # Your code
  return(output)
}

在您的示例中,我确定了Table

4。提取所有导入/库并将它们放在功能之外

library(lib1)
my_function <- function(arg1, arg2, ...){
  # Your code
  return(output)
}

使用@ r2evans建议进行编辑:关于此文献,通常使用library代替requireherehere。 在您的代码中,我确定了tidyrtibble

5。确定要打印的内容 /视图以及仅用于调试的内容。添加要打印的打印品,使您不需要的东西

6。在代码中添加一些注释/切片

例如,我会添加类似# Creating XXX table

7。提高代码质量

您应尽量减少代码行的数量(例如,使用循环并避免代码成倍增加)。使变量名称明确(而不是k,u,r ...)

关于循环,您可以在代码中一次放置一些列,可以循环执行以按顺序删除它们。 (这是我下面所做的)。它有助于使您的代码更易于阅读/调试。在这种特殊情况下,正如Gregor所说的,使用列名列表一次将它们全部删除(如果您有兴趣的话,请查看他的评论)是天作之合。


您在这里:

特别是在第7点和第5点方面,还有一些改进要做。

library(tibble)
library(tidyr)

yelp_function <- function(yelp){
  # Printing the input
  print(head(yelp, 10))
  print(str(yelp))

  # Flatten table
  yelp_flat<- flatten(yelp)
  print(str(yelp_flat))

  # Create yelp_tbl and drop some columns
  yelp_tbl <- as_data_frame(yelp_flat)
  # Drop some columns
  for (col in c("newcolumn", "newcolumn1", "shotClock", "period", "wallClock", "gameClock", "gameClockStopped", "ball")){
    yelp_tbl[, col] <- NULL
  }

  print(head(yelp_tbl))

  # Build some table
  good <- unnest(yelp_tbl) #extracts xyz from original dataframe
  player <- good %>% separate(xyz, c("player_x", "player_y", "player_z"), sep = ",")
  finish <- player %>% separate(xyz1, c("player_x", "player_y", "player_z"), sep = ",")
  k <- finish %>% separate(player_x, c("trash", "player_x"), sep = "c")
  k$trash <- NULL
  r <- k %>% separate(player_z, c("player_z", "tra"), sep = "\\)")
  u <- r %>% separate(player_x, c("kol", "player_x"), sep = "\\(")

  # Build away team
  Away_Team <- u

  # Build yelp table: I'm not quite sure why you are rebdoing that... Is this code necessary?
  yelp_tbl
  # Drop some columns
  for (col in c("newcolumn", "newcolumn1", "shotClock", "period", "wallClock", "gameClock", "gameClockStopped", "ball")){
    yelp_tbl[, col] <- NULL
  }

  print(head(yelp_tbl))
  good <- unnest(yelp_tbl) #extracts xyz from original dataframe

  # Build some table
  player <- good %>% separate(xyz, c("player_x", "player_y", "player_z"), sep = ",")
  finish <- player %>% separate(xyz1, c("player_x", "player_y", "player_z"), sep = ",")
  k <- finish %>% separate(player_x, c("trash", "player_x"), sep = "c")
  k$trash <- NULL
  r <- k %>% separate(player_z, c("player_z", "tra"), sep = "\\)")
  u <- r %>% separate(player_x, c("kol", "player_x"), sep = "\\(")

  ## Build home_team
  Home_Team <- u

  # Drop some columns
  for (col in c("garbage", "playerId1", "aplayer_x", "aplayer_y", "aplayer_z", "dispose", "brack", "kol", "tra")){
    Away_Team[, col] <- NULL
    Home_Team[, col] <- NULL
  }

  # Merge
  Table <- rbind(Home_Team, Away_Team)

  # Return
  return(Table)
}

View(Table) #order frameIdx to see correct order

运行它:

要运行您的代码,现在只需使用所需的参数执行该函数即可:

yelp_function(yelp)

注意1:,请注意,我没有测试代码,因为您没有提供运行它的数据。为了改善您的问题,您应该使用dput函数提供一些数据。

注意2:代码中总有改进的余地,因此您可能想进一步深入研究重构,以避免代码重复。通过一些健全性检查来控制您的输入...

答案 1 :(得分:-1)

这很简单。

您这样做:

foo <- function{
   #all your code goes here
}

然后您通过键入(例如在控制台中)来调用函数:

foo()