英文:
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 <- 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" )
)
)
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 <- function(id) {
if( !("s" %in% colnames(id)))
mutate(id, s = "")
else {do nothing}
}
List2 <- 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]] = ''
df
}
Apply this function twice to each element of List1:
map(List1, function(df) {
df %>% add_column('s') %>% add_column('t')
})
答案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 <- function(df,cols) {
for(col in cols) {
if(is.null(df[[col]])) df[[col]] <- ''
}
return(df)
}
Your lapply:
lapply(List1, checkAdd, cols= c('s','t'))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论