6
I try to run a function to rename variables from data.frame
s of a list
and get only error messages.
When I run the function for a database the analysis takes place:
library(tidyverse)
tibble=structure(list(var1 = c(28.5627505742013, 22.8311421908438, 95.2216156944633,
43.9405107684433, 97.11211245507, 48.4108281508088), var2 = c(32.9009465128183,
54.1136392951012, 69.3181485682726, 70.2100433968008, 44.0986660309136,
62.8759404085577), var3 = c(89.6971945464611, 67.174579706043,
37.0924087055027, 87.7977314218879, 29.3221596442163, 37.5143952667713
), var4 = c(41.5336912125349, 98.2095112837851, 80.7970978319645,
91.1278881691396, 66.4086666144431, 69.2618868127465), var5 = c(33.9312525652349,
88.1815139763057, 98.4453701227903, 25.0217059068382, 41.1195872165263,
37.0983888953924), var6 = c(39.813664201647, 80.6405956856906,
30.0273275375366, 34.6203793399036, 96.5195455029607, 44.5830867439508
), kmeans = structure(c(2L, 1L, 3L, 1L, 3L, 1L), .Label = c("1",
"2", "3"), class = "factor"), group = structure(c(1L, 1L, 1L,
1L, 1L, 1L), .Label = c("a", "b", "c", "d"), class = "factor")), .Names =
c("var1",
"var2", "var3", "var4", "var5", "var6", "kmeans", "group"), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))
Function:
names(tibble)[str_which(names(tibble),regex('KMEaNS',ignore_case=TRUE))]<-'variable'
In the case of a list
, the result is not returned:
mylist=structure(list(dataset1 = structure(list(var1 = c(28.5627505742013,
22.8311421908438, 95.2216156944633, 43.9405107684433, 97.11211245507,
48.4108281508088), var2 = c(32.9009465128183, 54.1136392951012,
69.3181485682726, 70.2100433968008, 44.0986660309136, 62.8759404085577
), var3 = c(89.6971945464611, 67.174579706043, 37.0924087055027,
87.7977314218879, 29.3221596442163, 37.5143952667713), var4 = c(41.5336912125349,
98.2095112837851, 80.7970978319645, 91.1278881691396, 66.4086666144431,
69.2618868127465), var5 = c(33.9312525652349, 88.1815139763057,
98.4453701227903, 25.0217059068382, 41.1195872165263, 37.0983888953924
), var6 = c(39.813664201647, 80.6405956856906, 30.0273275375366,
34.6203793399036, 96.5195455029607, 44.5830867439508), kmeans = structure(c(2L,
1L, 3L, 1L, 3L, 1L), .Label = c("1", "2", "3"), class = "factor"),
group = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("a",
"b", "c", "d"), class = "factor")), .Names = c("var1", "var2",
"var3", "var4", "var5", "var6", "kmeans", "group"), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame")), dataset2 = structure(list(
var1 = c(28.5627505742013, 22.8311421908438, 95.2216156944633,
43.9405107684433, 97.11211245507, 48.4108281508088), var2 = c(32.9009465128183,
54.1136392951012, 69.3181485682726, 70.2100433968008, 44.0986660309136,
62.8759404085577), var3 = c(89.6971945464611, 67.174579706043,
37.0924087055027, 87.7977314218879, 29.3221596442163, 37.5143952667713
), var4 = c(41.5336912125349, 98.2095112837851, 80.7970978319645,
91.1278881691396, 66.4086666144431, 69.2618868127465), var5 = c(33.9312525652349,
88.1815139763057, 98.4453701227903, 25.0217059068382, 41.1195872165263,
37.0983888953924), var6 = c(39.813664201647, 80.6405956856906,
30.0273275375366, 34.6203793399036, 96.5195455029607, 44.5830867439508
), kmeans = structure(c(2L, 1L, 3L, 1L, 3L, 1L), .Label = c("1",
"2", "3"), class = "factor"), group = structure(c(1L, 1L,
1L, 1L, 1L, 1L), .Label = c("a", "b", "c", "d"), class = "factor")), .Names =
c("var1",
"var2", "var3", "var4", "var5", "var6", "kmeans", "group"), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame")), dataset3 = structure(list(
var1 = c(28.5627505742013, 22.8311421908438, 95.2216156944633,
43.9405107684433, 97.11211245507, 48.4108281508088), var2 = c(32.9009465128183,
54.1136392951012, 69.3181485682726, 70.2100433968008, 44.0986660309136,
62.8759404085577), var3 = c(89.6971945464611, 67.174579706043,
37.0924087055027, 87.7977314218879, 29.3221596442163, 37.5143952667713
), var4 = c(41.5336912125349, 98.2095112837851, 80.7970978319645,
91.1278881691396, 66.4086666144431, 69.2618868127465), var5 = c(33.9312525652349,
88.1815139763057, 98.4453701227903, 25.0217059068382, 41.1195872165263,
37.0983888953924), var6 = c(39.813664201647, 80.6405956856906,
30.0273275375366, 34.6203793399036, 96.5195455029607, 44.5830867439508
), kmeans = structure(c(2L, 1L, 3L, 1L, 3L, 1L), .Label = c("1",
"2", "3"), class = "factor"), group = structure(c(1L, 1L,
1L, 1L, 1L, 1L), .Label = c("a", "b", "c", "d"), class = "factor")), .Names =
c("var1",
"var2", "var3", "var4", "var5", "var6", "kmeans", "group"), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))), .Names = c("dataset1",
"dataset2", "dataset3"))
Function 1:
mylist%>%
lapply(function(x){
lapply(x,names(x)[str_which(names(x),regex('KMEa',ignore_case=T))])<-'variable'
})
Error in lapply(x, Names(x)[str_which(Names(x), regex("Kmea", ignore_case = T)]) <- "variable" : could not find Function "lapply<-"
Function 2:
mylist%>%
map(.,~names(.)[str_which(names(.),regex('KM',ignore_case=T))])<-'variable'
Error in Mylist %>% map(., ~Names(.)[str_which(Names(.), regex("KM", ignore_case = T)]) <- "variable" : could not find Function "%>%<-"
Two questions:
which errors in the two codes?
there is some similar solution with the
dplyr::rename
?
you can give a simple in
stringr::str_replace_all(...)
in the argumentpattern
without having to usestringr::regex()
because of the argumentignore_case = T
. Just use the special character "(?i)" inpattern
which denotes ignoring whether the character is uppercase or lowercase (see my answer).– JdeMello