[英]Rename_if column in R with prefix if name %in% list: lists do not match
I have a list of tibbles which have been created with map
functions.我有一个使用map
函数创建的小标题列表。 The column names are as follows...列名如下...
dfA[[1]]%>% colnames(.)
[1] "date_strt" "grouped_id" "10938_0" "12881_0"
The numbered column names create issues for a function I am using (mice), and I need to change them to have a string prefixed to the beginning.编号的列名称为我正在使用的函数(鼠标)创建问题,我需要将它们更改为在开头添加一个字符串。 As I need to do this for a large number of tibbles I cannot reference the names directly.由于我需要为大量小标题执行此操作,因此我无法直接引用名称。 So I have created lists of character vectors from another tibbles names
所以我从另一个 tibbles names
创建了字符向量列表
df_list <- dfB %>% names() %>% as.vector()
Some of the names have the string "ITEM" prefixed, so I remove this for the matching list.一些名称以字符串“ITEM”为前缀,因此我将其删除以用于匹配列表。
df_list <- purrr::map(df_list ,function(x){x %>% str_remove(.,"ITEM")})
When I attempt to then append according to the strings, we get some unwanted special characters当我尝试根据字符串进行追加时,我们会得到一些不需要的特殊字符
purrr::map2(dfA, df_list, function(df, name_list)
df %>% rename_if(colnames(.) %in% name_list, ~paste0("ITEM", .)))
[[1]] date_strt grouped_id `ITEM\`12881_0\`` `ITEM\`79916_0\``
I then use list
to convert the name list (df_list) to list, as I have been told this is an issue.然后我使用list
将名称列表 (df_list) 转换为 list,因为有人告诉我这是一个问题。
df_list<- list(df_list)
This worked last night, however for some reason after restarting my session today (and updating my packages) it no longer works.这在昨晚有效,但是由于某种原因,在今天重新启动我的会话(并更新我的包)后,它不再有效。
When I do a conditional check of row names当我对行名进行条件检查时
dfA[[1]][-1]%>% colnames(.) == df_list[[1]]
I get this before applying list
我在申请list
之前得到了这个
[1] FALSE TRUE TRUE
But this after但这之后
[1] FALSE FALSE FALSE
I do no know why but df_list[[1]] now returns both list times rather than just list [1]我不知道为什么,但是 df_list[[1]] 现在返回两个列表时间,而不仅仅是列表 [1]
before apply list
申请前list
[1] "day" "10938_0" "12881_0"
after apply list
申请后list
[[1]]
[1] "day" "10938_0" "12881_0"
[[2]]
[1] "day" "12881_0" "79916_0"
Any suggestions as to why and how to fix it?关于为什么以及如何解决它的任何建议? Alternatively, its only columns which start with numbers that I need to append the prefix too, but I can't see an obvious solution.或者,它唯一以数字开头的列也需要附加前缀,但我看不到明显的解决方案。
here the str_remove
is applied on the entire dataset and according to ?str_remove
the input string
would be这里str_remove
应用于整个数据集,根据?str_remove
输入string
将是
string - Input vector.字符串 - 输入向量。 Either a character vector, or something coercible to one要么是一个字符向量,要么是某种强制的东西
data.frame/tibble
are not coercible to vector
data.frame/tibble
不能data.frame/tibble
为vector
This can be reproduced with a simple example这可以用一个简单的例子重现
head(iris) %>%
str_remove("\\..*")
#[1] "c(5" "c(3" "c(1" "c(0" "c(1, 1, 1, 1, 1, 1)"
instead it should be applied on each column相反,它应该应用于每一列
library(dplyr)
library(purrr)
library(stringr)
purrr::map(df_list, ~ .x %>%
rename_all(~ str_remove(.,"ITEM")))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.