在一个数据框列表中有条件地创建字段。

huangapple go评论75阅读模式
英文:

Conditional creation of fields in a list of dataframes r

问题

I can help with the translation:

我有一个包含数据框的列表,想要测试这些数据框是否包含特定字段;如果没有,应该创建这些字段(为空字段),当字段存在时,不应该进行任何操作(保持值不变)。

示例:

List1 <- list(
  DF1 = data.frame("x"=c("A","B","C"),
             "y"=c("D","E","F"),
             "z"=c("G","H","I" )
             ),
  DF2 = data.frame("t"=c("K","L","M"),
                   "y"=c("D","E","F"),
                   "z"=c("G","H","I" )
  )
) 

在这些数据框上,我想要测试字段"s"和"t"是否存在。字段"s"应该在DF1和DF2中创建,字段"t"应该仅在DF1中创建。

我尝试创建单独的函数(一个用于"s",一个用于"t"),但无法使其正常工作。此外,我想知道是否可以在一个函数中完成所有测试。请参见我尝试的函数结构(用于"s"的部分):

Existence_col_s <- function(id) {
  if (!("s" %in% colnames(id)))
    mutate(id, s = "")
  else {do nothing}
}

List2 <- lapply(List1, c(Existence_col_s, Existence_col_t))

有什么建议吗?

英文:

I am having a list with dataframes and want to test if the dataframes include certain fields; if not these fields should be created (as blank field), when the field exists it should do nothing (leave the values unchanged).

Example:

List1 &lt;- list(
  DF1 = data.frame(&quot;x&quot;=c(&quot;A&quot;,&quot;B&quot;,&quot;C&quot;),
             &quot;y&quot;=c(&quot;D&quot;,&quot;E&quot;,&quot;F&quot;),
             &quot;z&quot;=c(&quot;G&quot;,&quot;H&quot;,&quot;I&quot; )
             ),
  DF2 = data.frame(&quot;t&quot;=c(&quot;K&quot;,&quot;L&quot;,&quot;M&quot;),
                   &quot;y&quot;=c(&quot;D&quot;,&quot;E&quot;,&quot;F&quot;),
                   &quot;z&quot;=c(&quot;G&quot;,&quot;H&quot;,&quot;I&quot; )
  )
) 

On these dataframes I want to test if field "s" and "t" exist. Field "s" should be created in DF1 and DF2, Field "t" should be created only in DF1.

I tried the creation of separate functions (one for "s" and one for "t" but wasn't able to get it working properly. Furthermore I wonder if I can do all the test in one function. See below the structure of the function I tried (for "s")

Existence_col_s &lt;- function(id) {
  if( !(&quot;s&quot; %in% colnames(id)))
    mutate(id, s = &quot;&quot;)
  else {do nothing}
}

List2 &lt;- lapply(List1, c(Existence_col_s, Existence_col_t))

Any ideas?

答案1

得分: 1

Helper function to conditionally add a column to a data.frame:

library(tidyverse)
add_column = function(df, col) {
  if (!col %in% names(df)) df[[col]] = ''
  df
}

Apply this function twice to each element of List1:

map(List1, function(df) {
  df %>% add_column('s') %>% add_column('t')
})
英文:

Helper function to conditionally add a column to a data.frame:

library(tidyverse)
add_column = function(df, col) {
  if (!col %in% names(df)) df[[col]] = &#39;&#39;
  df
}

Apply this function twice to each element of List1:

map(List1, function(df) {
  df %&gt;% add_column(&#39;s&#39;) %&gt;% add_column(&#39;t&#39;)
})

答案2

得分: 1

这是一个基本解决方案:

一个回调来检查并添加列:

checkAdd <- function(df, cols) {
  for(col in cols) {
    if(is.null(df[[col]])) df[[col]] <- ''
  }
  return(df)
}

您的lapply:

lapply(List1, checkAdd, cols = c('s', 't'))
英文:

Here's a base solution:

A callback to check and add columns:

checkAdd &lt;- function(df,cols) {
  for(col in cols) {
    if(is.null(df[[col]])) df[[col]] &lt;- &#39;&#39;
  }
  return(df)
}

Your lapply:

lapply(List1, checkAdd, cols= c(&#39;s&#39;,&#39;t&#39;))

huangapple
  • 本文由 发表于 2020年1月6日 23:04:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614389.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定