有条件地向数据帧列表中的数据帧添加行

时间:2019-11-08 00:52:00

标签: r dataframe nested conditional-statements

我有一个数据帧列表,其中每个数据帧都有1或2行名为“ mis”或“ syn”(形成名为cat的列)以及第二个具有数字频率的列。我想填写每个数据帧,以便如果缺少“ mis”行,则添加频率为0的mis行 如果缺少“ syn”行,则添加频率为1的syn行:

###exmaple: 
#example list of dataframes:
df1<- as.data.frame(cbind(cat = c("mis", "syn"), freq= c(4, 2)))
df2<- as.data.frame(cbind(cat = "mis", freq= 1))
df3<- as.data.frame(cbind(cat = "syn", freq= 2))
df_list<- list(df1 = df1, df2 = df2, df3= df3)

看起来像:

> df_list
$df1
cat freq
1 mis    4
2 syn    2

$df2
cat freq
1 mis    1

$df3
cat freq
1 syn    2

预期输出:

> df_list
$df1
cat freq
mis    4
syn    2

$df2
cat freq
mis    1
syn    1

$df3
cat freq
syn    2
mis    0

我尝试过的方法: 首先,我更改行名称,以便可以通过它们进行搜索

df_list_named<- lapply(df_list, function(x){ row.names(x)<-as.character(x$cat); x})

df_list_named
$df1
cat freq
mis mis    4
syn syn    2

$df2
cat freq
mis mis    1

$df3
cat freq
syn syn    2

然后我一直在尝试使用ifelse循环将行追加到需要它的数据帧中,但是我无法使其工作:

test<- lapply(df_list_named, function (x) ifelse(!row.names(df_list_named[[x]]) %in% "mis", rbind(df_list_named[[x]], c(cat = "mis", freq= 0)), 
                                                                                               ifelse(!row.names(df_list_named[[x]]) %in% "syn", rbind(df_list_named[[x]], c(cat = "syn", freq= 1))))

2 个答案:

答案 0 :(得分:2)

这是使用lapply

的一种方法
lapply(df_list, function(x) {
   if(all(c("mis", "syn") %in% x$cat))
      x
   else if("mis" %in% x$cat)
      rbind(x, data.frame(cat = "syn", freq = 1))
   else
      rbind(x, data.frame(cat = "mis", freq = 0))
})

#$df1
#  cat freq
#1 mis    4
#2 syn    2

#$df2
#  cat freq
#1 mis    1
#2 syn    1

#$df3
#  cat freq
#1 syn    2
#2 mis    0

数据

df1<- data.frame(cat = c("mis", "syn"), freq= c(4, 2), stringsAsFactors = FALSE)
df2<- data.frame(cat = "mis", freq= 1,stringsAsFactors = FALSE)
df3<- data.frame(cat = "syn", freq= 2, stringsAsFactors = FALSE)
df_list<- list(df1 = df1, df2 = df2, df3= df3)

答案 1 :(得分:0)

您可以使用"base"数据帧,将merge与列表中的所有数据帧一起使用Map。在已经完整的数据框中创建的duplicated行可以用!安全地排除,因为它们总是放在最后。

(base <- data.frame(cat=factor(c("syn", "mis")), freq=factor(1:0)))
#   cat freq
# 1 syn    1
# 2 mis    0

Map(function(x) {y <- (merge(x, base, all=TRUE));y[!duplicated(y$cat), ]}, df_list)
# $df1
#   cat freq
# 1 mis    4
# 3 syn    2
# 
# $df2
#   cat freq
# 1 mis    1
# 3 syn    1
# 
# $df3
#   cat freq
# 1 syn    2
# 3 mis    0

数据

df_list <- list(df1 = structure(list(cat = structure(1:2, .Label = c("mis", 
"syn"), class = "factor"), freq = structure(2:1, .Label = c("2", 
"4"), class = "factor")), class = "data.frame", row.names = c(NA, 
-2L)), df2 = structure(list(cat = structure(c(cat = 1L), .Label = "mis", class = "factor"), 
    freq = structure(c(freq = 1L), .Label = "1", class = "factor")), class = "data.frame", row.names = c(NA, 
-1L)), df3 = structure(list(cat = structure(c(cat = 1L), .Label = "syn", class = "factor"), 
    freq = structure(c(freq = 1L), .Label = "2", class = "factor")), class = "data.frame", row.names = c(NA, 
-1L)))