如何使用gsub从R中的列表中删除部分

时间:2014-11-10 21:27:31

标签: r list gsub

所以这里有我的数据 -

$`cases-travel-associated`
  Country Total Cases Laboratory-Confirmed Cases Total Deaths
1    Mali           1                          1            1
2 Senegal          1*                         1*            0
3   Total           2                          2            1

 $`cases-localized-transmission`
    Country Total Cases Laboratory-Confirmed Cases Total Deaths
 1       Nigeria         20*                        19*            8
 2         Spain           1                          1            0
 3 United States           4                          4            1
 4         Total          25                         24            9

我想使用gsub -

删除所有的星号
> gsub("\\*", "", ebola[,3])

然后返回 -

Error in ebola[, 3] : incorrect number of dimensions

我想我知道问题是这个类是列表而不是数据帧,所以我不能写#ebola [,3]"但我不知道如何解决这个问题,以删除所有的星号。谁能告诉我应该怎么做?

非常感谢!

> dput(ebola)
structure(list(`cases-widespread` = structure(list(Country = structure(1:4, .Label = c("Guinea", 
"Liberia", "Sierra Leone", "Total"), class = "factor"), `Total Cases` = structure(c(2L, 
4L, 3L, 1L), .Label = c("13241", "1760", "4862", "6919"), class = "factor"), 
`Laboratory-Confirmed Cases` = structure(1:4, .Label = c("1479", 
"2514", "4149", "8142"), class = "factor"), `Total Deaths` = structure(c(1L, 
3L, 2L, 4L), .Label = c("1054", "1130", "2766", "4950"), class = "factor")), .Names = c("Country", 
"Total Cases", "Laboratory-Confirmed Cases", "Total Deaths"), row.names = c(NA, 
-4L), class = "data.frame"), `cases-travel-associated` = structure(list(
Country = structure(1:3, .Label = c("Mali", "Senegal", "Total"
), class = "factor"), `Total Cases` = structure(1:3, .Label = c("1", 
"1*", "2"), class = "factor"), `Laboratory-Confirmed Cases` = structure(1:3, .Label = c("1", 
"1*", "2"), class = "factor"), `Total Deaths` = structure(c(2L, 
1L, 2L), .Label = c("0", "1"), class = "factor")), .Names = c("Country", 
"Total Cases", "Laboratory-Confirmed Cases", "Total Deaths"), row.names = c(NA, 
-3L), class = "data.frame"), `cases-localized-transmission` = structure(list(
Country = structure(c(1L, 2L, 4L, 3L), .Label = c("Nigeria", 
"Spain", "Total", "United States"), class = "factor"), `Total Cases` = structure(c(2L, 
1L, 4L, 3L), .Label = c("1", "20*", "25", "4"), class = "factor"), 
`Laboratory-Confirmed Cases` = structure(c(2L, 1L, 4L, 3L
), .Label = c("1", "19*", "24", "4"), class = "factor"), 
`Total Deaths` = structure(c(3L, 1L, 2L, 4L), .Label = c("0", 
"1", "8", "9"), class = "factor")), .Names = c("Country", 
"Total Cases", "Laboratory-Confirmed Cases", "Total Deaths"), row.names = c(NA, 
-4L), class = "data.frame")), .Names = c("cases-widespread", 
"cases-travel-associated", "cases-localized-transmission"))

1 个答案:

答案 0 :(得分:1)

您可以使用以下方法删除所有*

ebola <- lapply(ebola, function(d) {
  as.data.frame(lapply(d, gsub, pattern = "\\*", replacement = ""))
})
# $`cases-widespread`
#        Country Total.Cases Laboratory.Confirmed.Cases Total.Deaths
# 1       Guinea        1760                       1479         1054
# 2      Liberia        6919                       2514         2766
# 3 Sierra Leone        4862                       4149         1130
# 4        Total       13241                       8142         4950
# 
# $`cases-travel-associated`
#   Country Total.Cases Laboratory.Confirmed.Cases Total.Deaths
# 1    Mali           1                          1            1
# 2 Senegal           1                          1            0
# 3   Total           2                          2            1
# 
# $`cases-localized-transmission`
#         Country Total.Cases Laboratory.Confirmed.Cases Total.Deaths
# 1       Nigeria          20                         19            8
# 2         Spain           1                          1            0
# 3 United States           4                          4            1
# 4         Total          25                         24            9