我有以下示例数据:
DF
val_str
fruit=apple,machine=crane
machine=crane
machine=roboter
fruit=apple
machine=roboter,food=samosa
DF2
fruit machine food
apple crane NA
NA crane NA
NA roboter NA
apple NA NA
NA roboter samosa
如何从df到达df2? " ="之前的每个唯一值应该创建一个列,然后属于它的相应值应该分布在各行。
代码:
df <- data.frame(val_str = c("fruit=apple,machine=crane","machine=crane","machine=roboter", "fruit=apple", "machine=roboter,food=samosa"))
df2 <- data.frame(fruit = c("apple",NA,NA,"apple","NA"),
machine = c("crane","crane","roboter",NA,"roboter"),
food = c(NA,NA,NA,NA,"samosa"))
答案 0 :(得分:1)
我们可以在'val_str'列上执行strsplit
,通过循环遍历data.frame
元素,通过list
元素从备用元素创建map
(使用逻辑索引通过回收进行子搜索) {1}}
library(dplyr)
library(purrr)
strsplit(as.character(df$val_str), "[=,]") %>%
map_df(~ setNames(as.data.frame.list(.[c(FALSE, TRUE)]), .[c(TRUE, FALSE)]))
# fruit machine food
#1 apple crane <NA>
#2 <NA> crane <NA>
#3 <NA> roboter <NA>
#4 apple <NA> <NA>
#5 <NA> roboter samosa