I try to rename the dfs in my list by the first Element of a specific column (SurveyId) of my dataframes within the list. I tried:
extract2_PR1 <- lapply(extract2_PR, setNames, x[1,"SurveyIds"])
Where extract2_PR is my list of 16 Dfs, all of them containing the column SurveyId. But get the error message:
Error in FUN(X[[i]], ...) : object 'x' not found
An dput example of my dfs in my list is:
dput(extract2_PR[[1]][1:3, 1:23])
structure(list(hhid = c(" 1 27", " 1 27", " 1 27"
), hv001 = c(1, 1, 1), hv002 = c(27, 27, 27), hv005 = c(1707326,
1707326, 1707326), hv006 = c(8, 8, 8), hv007 = c(2003, 2003,
2003), hv021 = c(1, 1, 1), hv023 = structure(c(6, 6, 6), label = "Stratification used in sample design", format.spss = "F2.0", display_width = 7L, labels = c(`Tigray urban` = 1,
`Tigray rural` = 2, `Affar urban` = 3, `Affar rural` = 4, `Amhara urban` = 5,
`Amhara rural` = 6, `Oromiya urban` = 7, `Oromiya rural` = 8,
`Somali urban` = 9, `Somali rural` = 10, `Benishangul-Gumuz urban` = 11,
`Benishangul-Gumuz rural` = 12, `S.N.N.P. urban` = 13, `S.N.N.P. rural` = 14,
`Gambela urban` = 15, `Gambela rural` = 16, `Harari urban` = 17,
`Harari rural` = 18, `Addis Ababa` = 19, `Dire Dawa urban` = 20,
`Dire Dawa rural` = 21, `Somali oversample urban` = 22, `Somali oversample rural` = 23
), class = c("haven_labelled", "vctrs_vctr", "double")), hv024 = structure(c(3,
3, 3), label = "Region", format.spss = "F2.0", display_width = 7L, labels = c(Tigray = 1,
Affar = 2, Amhara = 3, Oromiya = 4, Somali = 5, `Benishangul-Gumuz` = 6,
SNNP = 7, Gambela = 12, Harari = 13, `Addis Ababa` = 14, `Dire Dawa` = 15
), class = c("haven_labelled", "vctrs_vctr", "double")), hv025 = structure(c(2,
2, 2), label = "Type of place of residence", format.spss = "F1.0", display_width = 7L, labels = c(Urban = 1,
Rural = 2), class = c("haven_labelled", "vctrs_vctr", "double"
)), hc1 = c(NA_real_, NA_real_, NA_real_), hc70 = structure(c(NA_real_,
NA_real_, NA_real_), label = "Height/Age standard deviation (new WHO)", na_values = 9999, format.spss = "F4.0", display_width = 6L, labels = c(`Height out of plausible limits` = 9996,
`Age in days out of plausible limits` = 9997, `Flagged cases` = 9998
), class = c("haven_labelled_spss", "haven_labelled", "vctrs_vctr",
"double")), hc71 = structure(c(NA_real_, NA_real_, NA_real_), label = "Weight/Age standard deviation (new WHO)", na_values = 9999, format.spss = "F4.0", display_width = 6L, labels = c(`Height out of plausible limits` = 9996,
`Age in days out of plausible limits` = 9997, `Flagged cases` = 9998
), class = c("haven_labelled_spss", "haven_labelled", "vctrs_vctr",
"double")), hc72 = structure(c(NA_real_, NA_real_, NA_real_), label = "Weight/Height standard deviation (new WHO)", na_values = 9999, format.spss = "F4.0", display_width = 6L, labels = c(`Height out of plausible limits` = 9996,
`Age in days out of plausible limits` = 9997, `Flagged cases` = 9998
), class = c("haven_labelled_spss", "haven_labelled", "vctrs_vctr",
"double")), hc73 = structure(c(NA_real_, NA_real_, NA_real_), label = "BMI standard deviation (new WHO)", na_values = 9999, format.spss = "F4.0", display_width = 6L, labels = c(`Height out of plausible limits` = 9996,
`Age in days out of plausible limits` = 9997, `Flagged cases` = 9998
), class = c("haven_labelled_spss", "haven_labelled", "vctrs_vctr",
"double")), hv103 = structure(c(1, 1, 1), label = "Slept last night", na_values = 9, format.spss = "F1.0", display_width = 7L, labels = c(No = 0,
Yes = 1), class = c("haven_labelled_spss", "haven_labelled",
"vctrs_vctr", "double")), CLUSTER = c(1, 1, 1), ALT_DEM = c(2696L,
2696L, 2696L), LATNUM = c(10.193657, 10.193657, 10.193657), LONGNUM = c(39.345773,
39.345773, 39.345773), ADM1NAME = c("Amhara Region", "Amhara Region",
"Amhara Region"), DHSREGNA = c("amhara", "amhara", "amhara"),
SurveyId = c("ET2011DHS", "ET2011DHS", "ET2011DHS")), row.names = c(NA,
3L), class = "data.frame")
Thanks for help!
EDIT: OUTPUT (see answers comments)
unique(sapply(extract2_PR, `[[`, 'SurveyId'))
[[16]]
[1] "ZW2015DHS" "ZW2015DHS" "ZW2015DHS" "ZW2015DHS" "ZW2015DHS" "ZW2015DHS" "ZW2015DHS" "ZW2015DHS" "ZW2015DHS"
[10] "ZW2015DHS" "ZW2015DHS" "ZW2015DHS" "ZW2015DHS" "ZW2015DHS" "ZW2015DHS" "ZW2015DHS" "ZW2015DHS" "ZW2015DHS"
[19] "ZW2015DHS" "ZW2015DHS" "ZW2015DHS" "ZW2015DHS" "ZW2015DHS" "ZW2015DHS" "ZW2015DHS" "ZW2015DHS" "ZW2015DHS"
[28] "ZW2015DHS" "ZW2015DHS" "ZW2015DHS" "ZW2015DHS" "ZW2015DHS" "ZW2015DHS" "ZW2015DHS" "ZW2015DHS" "ZW2015DHS"
[37] "ZW2015DHS" "ZW2015DHS"
CodePudding user response:
You may use a function of x
that uses the first el
ement. Demonstrated on a minimal example:
extract2_PR <- setNames(extract2_PR, lapply(extract2_PR, \(x) el(x[, 'SurveyId'])))
extract2_PR
# $S1
# X1 X2 X3 X4 SurveyId
# 1 1 4 7 10 S1
# 2 2 5 8 11 S1
# 3 3 6 9 12 S1
#
# $S2
# X1 X2 X3 X4 SurveyId
# 1 1 4 7 10 S2
# 2 2 5 8 11 S2
# 3 3 6 9 12 S2
Data:
extract2_PR <- list(structure(list(X1 = 1:3, X2 = 4:6, X3 = 7:9, X4 = 10:12,
SurveyId = c("S1", "S1", "S1")), class = "data.frame", row.names = c(NA,
-3L)), structure(list(X1 = 1:3, X2 = 4:6, X3 = 7:9, X4 = 10:12,
SurveyId = c("S2", "S2", "S2")), class = "data.frame", row.names = c(NA,
-3L)))