我如何创建一个循环以更改r中标签变量中标签的文本编码

发布于 2025-01-28 10:43:15 字数 880 浏览 5 评论 0原文

我已经导入了一个Stata文件,该文件正在为我提供了值标签中的一些编码问题。在导入时,使用标记:: lookfor对于任何关键字返回以下错误:

Error in structure(as.character(x), names = names(x)) : 
  invalid multibyte string at '<e9>bec Solidaire'

知道数据集,几乎可以肯定是其中的值标签。

如何通过数据集循环使用值标签的名称,然后将其重置。我认为我已经找到了解决问题的字符的解决方案,但是我不知道如何替换原始名称。

v <- labelled(c(1,2,2,2,3,9,1,3,2,NA), c(yes = 1, "Bloc Qu\xe9b\xe9cois" = 3, "don't know" = 9))
x<- labelled(c(1,2,2,2,3,9,1,3,2,NA), c("Bloc Qu\xe9b\xe9cois" = 1, no = 3, "don't know" = 9))

mydat<-data.frame(v=v, x=x)

glimpse(mydat)
mydat %>% 
  map(., val_labels)
#This works individually
iconv(names(val_labels(x)), from="latin1", to="UTF-8")
#And this seems to work looping over each variable, but how to I store it?
mydat %>% 
  map(., function(x) iconv(names(val_labels(x)), from="latin1", to="UTF-8"))

I have imported a stata file that is giving me some encoding problems in the value labels. On import, using labelled::lookfor for any keyword returns this error:

Error in structure(as.character(x), names = names(x)) : 
  invalid multibyte string at '<e9>bec Solidaire'

Knowing the data-set, that is almost certainly a value label in there.

How to I loop through the data-set fixing the encoding problem in the names of the value labels and then reset them. I have found a solution, I think, to fix the problematic characters, but I don't know how to replace the original names.

v <- labelled(c(1,2,2,2,3,9,1,3,2,NA), c(yes = 1, "Bloc Qu\xe9b\xe9cois" = 3, "don't know" = 9))
x<- labelled(c(1,2,2,2,3,9,1,3,2,NA), c("Bloc Qu\xe9b\xe9cois" = 1, no = 3, "don't know" = 9))

mydat<-data.frame(v=v, x=x)

glimpse(mydat)
mydat %>% 
  map(., val_labels)
#This works individually
iconv(names(val_labels(x)), from="latin1", to="UTF-8")
#And this seems to work looping over each variable, but how to I store it?
mydat %>% 
  map(., function(x) iconv(names(val_labels(x)), from="latin1", to="UTF-8"))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不再见 2025-02-04 10:43:15

在一个简单的步骤中,这似乎有点困难,因此在这里我使用了一些辅助功能

conv_names <- function(x) {
  setNames(x, iconv(names(x), from="latin1", to="UTF-8"))
}
conv_val_labels <- function(x) {
  val_labels(x) <- conv_names(val_labels(x))
  x
}

mydat <- map_dfc(mydat, conv_val_labels)

,但是我们将功能映射到每个列,然后将这些列重新分配回数据框架。注意我们使用map_dfc将列组合回数据框架

This seems to be a bit tough to do in one simple step, so here I used some helper functions

conv_names <- function(x) {
  setNames(x, iconv(names(x), from="latin1", to="UTF-8"))
}
conv_val_labels <- function(x) {
  val_labels(x) <- conv_names(val_labels(x))
  x
}

mydat <- map_dfc(mydat, conv_val_labels)

But we map the function to each column and then reassign those columns back to the data frame. Note we use map_dfc to combine the columns back into a data frame

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文