我有一个要合并在一起的59个数据框的列表。不幸的是,由于我已经抓取了许多数据,因此数据框中的列具有不同的类。它们都具有“名称”列,其中一些采用因子形式,有些采用字符形式。我想将它们全部更改为字符形式。我尝试了以下
dts <- c("Alabama","Alaska","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","Florida",
"Georgia","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine",
"Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska",
"Nevada","New_Hampshire","New_Jersey","New_Mexico","New_York","North_Carolina","North_Dakota",
"Ohio","Oklahoma","Oregon","Pennsylvania","Rhode_Island","South_Carolina","South_Dakota","Tennessee",
"Texas","Utah","Vermont","Virginia","Washington","West_Virginia","Wisconsin","Wyoming","Federal",
"CCJail","DC","LAJail","NOLA","NYCJail","OCJail","PhilJail","TXJail")
for(i in 1:length(dts)){
dts[i]$Name <- as.character(dts[i]$Name)
}
但是它只给我一个错误“错误:$运算符对原子向量无效”。 有谁知道一个好的解决方法?预先感谢您的帮助!
我的最终目标是跑步
dta <-dplyr::bind_rows(Alabama,Alaska,Arizona,Arkansas,California,Colorado,Connecticut,Delaware,Florida,
Georgia,Hawaii,Idaho,Illinois,Indiana,Iowa,Kansas,Kentucky,Louisiana,Maine,
Maryland,Massachusetts,Michigan,Minnesota,Mississippi,Missouri,Montana,Nebraska,
Nevada,New_Hampshire,New_Jersey,New_Mexico,New_York,North_Carolina,North_Dakota,
Ohio,Oklahoma,Oregon,Pennsylvania,Rhode_Island,South_Carolina,South_Dakota,Tennessee,
Texas,Utah,Vermont,Virginia,Washington,West_Virginia,Wisconsin,Wyoming,Federal,CCJail,
DC,LAJail,NOLA,NYCJail,OCJail,PhilJail,TXJail)
但是我收到错误消息“错误:无法将..1$Residents.Confirmed
和..2$Residents.Confirmed
结合在一起。”每个数据帧中都有许多列,而且它们经常是不同的类。如果有人有一个更好的解决方案,我也愿意接受!谢谢!
答案 0 :(得分:1)
我们可以使用list
将数据集加载到mget
中(假设数据集对象已在全局环境中创建),然后使用{{1}遍历list
},更改map
中“名称”列的class
,并在mutate
中用后缀_dfr
行绑定
map
如果有许多不同的library(dplyr)
library(purrr)
out <- map_dfr(mget(dts), ~ .x %>%
mutate(Name = as.character(Name)))
列。可能是,最好将所有列都转换为单个类,然后进行绑定
class
如果out <- map_dfr(mget(dts), ~ .x %>%
mutate(across(everything(), as.character)))
out <- type.convert(out, as.is = TRUE)
版本为dplyr
,请使用< 1.0.0
mutate_all
答案 1 :(得分:1)
d1 <- data.frame(
Name = as.factor(c("name1", "name2")),
Residents.Confirmed = c(0,1)
)
d2 <- data.frame(
Name = c("name3", "name4"),
Residents.Confirmed = c(2,3)
)
dataframes_list <- list(d1, d2)
for(i in 1:length(dataframes_list)){
dataframes_list[[i]]$Name <- as.character(dataframes_list[[i]]$Name)
}
bind_rows(dataframes_list)
答案 2 :(得分:0)
Base R解决方案:
type.convert(do.call("rbind",
Map(function(x){data.frame(lapply(x, as.character))}, dataframes_list)))
数据感谢@ chase171:
d1 <- data.frame(
Name = as.factor(c("name1", "name2")),
Residents.Confirmed = c(0,1)
)
d2 <- data.frame(
Name = c("name3", "name4"),
Residents.Confirmed = c(2,3)
)
dataframes_list <- list(d1, d2)