Is there a function/easy way for removing all spaces only in elements of dataset that contain numbers (class character)? For example "23 456"
Here's an example of the data:
structure(list(`VÝKAZ MYSL 1-01 - SUMÁŘE` = c("Vlastnický vztah k honitbě",
"1. vlastní", "2. společenstevní", NA, "Způsob využívání honitby",
"1. ve vlastní režii"), ...2 = c("počet", "1 496", "4 255", "honitba",
"počet", "751"), ...3 = c(NA, "1 511 605", "5 077 050", NA, NA,
NA), ...4 = c("ha", NA, NA, NA, "ha", "780 487"), ...5 = c("počet",
"178", "24", NA, "počet", "137"), ...6 = c(NA, NA, NA, "obora",
NA, NA), ...7 = c("ha", "41 911", "2 401", NA, "ha", "31 343"
), `3.9.2018` = c("počet", "20", "12", "bažantnice (s)", "počet",
"14"), ...9 = c("ha", "16 239", "6 446", NA, "ha", "10 294"),
...10 = c("počet", "25", "233", "bažantnice (h)", "počet",
"27"), ...11 = c("ha", "12 399", "60 782", NA, "ha", "12 047"
), ...12 = c(NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_), ...13 = c(NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_)), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
CodePudding user response:
Do you mean something like this?
ischr <- sapply(dat, is.character)
dat[ischr] <- lapply(dat[ischr], function(z) ifelse(grepl("[^ 0-9]", z), z, gsub(" ", "", z)))
dat
# # A tibble: 6 x 13
# `VÝKAZ MYSL 1-01 - SUMÁ E` ...2 ...3 ...4 ...5 ...6 ...7 `3.9.2018` ...9 ...10 ...11 ...12 ...13
# <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
# 1 "Vlastnický vztah k honitb " po et NA ha po et NA ha po et ha po et ha NA NA
# 2 "1. vlastní" 1496 1511605 NA 178 NA 41911 20 16239 25 12399 NA NA
# 3 "2. spole enstevní" 4255 5077050 NA 24 NA 2401 12 6446 233 60782 NA NA
# 4 NA honitba NA NA NA obora NA ba antnice (s) NA ba a~ NA NA NA
# 5 "Zp sob vyu ívání honitby" po et NA ha po et NA ha po et ha po et ha NA NA
# 6 "1. ve vlastní re ii" 751 NA 780487 137 NA 31343 14 10294 27 12047 NA NA