绑定R中的新行的函数

时间:2017-05-11 23:49:19

标签: r function dataframe append rows

我对R来说比较新,需要创建一个函数,在每次调用之后,一个数据帧将附加到另一行。

这是空数据帧应该是什么(也就是行的标题):

 Percent Category Word

功能是

output <- function(category, word)

最终会根据这些词来计算百分比。

因此,例如,类别(Lung,Female)将输出51的百分比,因此得到的数据框将是

Percent Category Word
51      Lung     Female

如果我要重新运行该功能,让我们说类别(Blood,Male),其百分比是71,我希望得到的数据帧是

Percent Category Word
51      Lung     Female
71      Blood    Male

我基本上想要按照我的意愿运行该函数,连续追加行。但是,数据帧只是本地的而不是全局的,因此当我运行该函数时,如果我要输出数据帧,那么该行只出现在控制台中,但在我的全局环境中没有显示任何内容。这就是我所拥有的(省略计算百分比的fxn的开头):

output <- function(category, word)
{
    row <- data.frame(percent, category, word)
    # row <- rbind(row, row) # I don't know...
    return (row)
}

鉴于该函数计算了一个百分比变量,我如何创建一个可从该函数向其追加行的全局环境中访问的数据框?我应该在全局环境中创建一个数据帧,然后尝试一下吗?

2 个答案:

答案 0 :(得分:0)

使用(sum(if [notification type] = "2" then [number of records] end) / sum(if [notification type] = "1" then [number of records] end))*100 计算data.frame的行数的解决方案。

nrow

输出:

df<-data.frame(percent=as.numeric(), category=as.character(), word=as.character(), 
stringsAsFactors = F) #setting up empty data.frame

output <- function(df,category, word){ 
  percent<-12 #fictious percentage
  df[nrow(df)+1,]<- data.frame(percent=percent, category=category, word=word, 
  stringsAsFactors = F) #adding the information to the next row of the data.frame
  return(df)
 }

 df<-output(df,"bla","bla")
 df<-output(df,"sdfd","dfsdf")
 df<-output(df,"xfdfd","qweqweqw")
 df<-output(df,"asdad","etwt")

答案 1 :(得分:0)

我建议在应聘家庭之后为您的思维建模。这样,您可以传递数据集,例如套用,并将一系列结构相似的数据帧绑定在一起。现在,从您的示例中假设这些百分比来自一个单独的数据框,以便创建一个示例:

#data to reference for percent calculations
data <- data.frame(
   category = c(rep('blood',5),rep('lung',5)), 
   word = c(rep('Male',3),rep('Female',4),rep('Male',3))
   )
#input for the function
input <- data.frame(
   category = c('blood','blood','lung'), 
   word = c('Male','Female','Male')
   )
#I'll use your function you have listed with some adjustments since I don't have your
#percent function.
output <- function(category_point, word_point, data){
    row <- data.frame(
    percent = sum(category_point == data[,'category'])/length(data[,'category']) ,
    category = category_point, word = word_point,stringsAsFactors = F)
    return (row)
}
#Now execute the apply function with the input being passed and execute by row.
list_of_rows = apply(input,1,function(x) output(x['category'],x['word'],data)) 

#bind this list of similarly structured rows together.
new_df  = bind_rows(list_of_rows)
new_df

  percent category   word
1     0.5    blood   Male
2     0.5    blood Female
3     0.5     lung   Male

我希望这会有所帮助!

编辑:我还应该提到,如果您想使用新输入再次运行它,则只需将新输入的结果连续bind_rows()连续输出到先前的输出即可。

new_input <- data.frame(
   category = c('lung','blood','lung'), 
   word = c('Female','Female','Female')
   )

#Run another apply
new_list_of_rows = apply(new_input,1,function(x) output(x['category'],x['word'],data)) 

#bind this list of similarly structured rows together.
new_new_df  = bind_rows(new_df,new_list_of_rows)
new_new_df

  percent category   word
1     0.5    blood   Male
2     0.5    blood Female
3     0.5     lung   Male
4     0.5     lung Female
5     0.5    blood Female
6     0.5     lung Female