0

So here's the data I have -

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

I wanna remove all the asterisks by using gsub -

> gsub("\\*", "", ebola[,3])

and it returns -

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

I think I know the problem is that the class is list instead of a dataframe, so I can't write "ebola[,3]", but I don't know how to fix this in order to remove all the asterisks. Can someone tell me what I should do?

Thanks a lot!

> 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"))
Anonymous
  • 41
  • 1
  • 4
  • 1
    paste the result of `dput(ebola)` into your question. – jlhoward Nov 10 '14 at 21:31
  • It looks like you are not working with a simple data.frame here. It looks like you have a list of data.frames. Is that what you want? If so, you'll need to gsub in each of them. Also, gsub does not change values in place. You'll need to reassign to the data.frame to update the values. – MrFlick Nov 10 '14 at 21:34
  • @MrFlick so i try ebola_df <- gsub("\\*", "", ebola$'cases-travel-associated'[,3]) it seems to work, because it returns - [1] "1" "1" "2" but it wouldn't update the dataframe after I remove it, so I don't know what I am supposed to do next. – Anonymous Nov 10 '14 at 21:36
  • @Anonymous It's much easier to provide a specific working answer when you provide a minimal [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – MrFlick Nov 10 '14 at 21:37
  • @jlhoward i just did. is that what you are referring to? thanks! – Anonymous Nov 10 '14 at 21:43

1 Answers1

2

You can remove all *s with this approach:

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
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168