我有一个数据框:
df <- data.frame(name = c("bob","joe"),
target = c("yellow", "grey"),
code1 = c("fly", "walk"),
code2 = c("jump", "run"))
Run Code Online (Sandbox Code Playgroud)
我想将信息写入具有特定结构的 yaml 文件:
samples:
bob:
target: yellow
code1: fly
code2: jump
joe:
target: grey
code1: walk
code2: run
Run Code Online (Sandbox Code Playgroud)
我无法使用这种特定结构实现 yaml 输出。
除了输出之外不使用任何包的版本yaml:
library(yaml)
out <- as.yaml(list(samples=split(replace(df, "name", NULL), df$name)))
# just to show it works:
cat(out)
#samples:
# bob:
# target: yellow
# code1: fly
# code2: jump
# joe:
# target: grey
# code1: walk
# code2: run
Run Code Online (Sandbox Code Playgroud)