【问题标题】:How to use gsub to remove parts from a list in R如何使用 gsub 从 R 中的列表中删除部分
【发布时间】:2014-11-10 21:27:31
【问题描述】:

这是我拥有的数据 -

$`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"))

【问题讨论】:

  • dput(ebola) 的结果粘贴到您的问题中。
  • 看起来您在这里没有使用简单的 data.frame。看起来您有一个 data.frames 列表。那是你要的吗?如果是这样,您将需要 gsub 在它们中的每一个中。此外, gsub 不会更改适当的值。您需要重新分配给 data.frame 以更新值。
  • @MrFlick 所以我尝试 ebola_df
  • @Anonymous 当您提供最小的reproducible example 时,提供具体的工作答案要容易得多。
  • @jlhoward 我刚刚做到了。这就是你所指的吗?谢谢!

标签: r list gsub


【解决方案1】:

您可以使用这种方法删除所有*s:

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

【讨论】:

  • 成功了!多谢!我有一个简单的问题,function(d) 中的“d”是什么?
  • @Anonymous d 代表数据框列表中的一个数据框。
猜你喜欢
  • 2018-10-28
  • 2016-02-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多