英文:
Convert data frame column values into multiple yaml file
问题
State:
type: string
example: A
Cust_Age:
type: number
example: 25
City:
type: string
example: Aa
State:
type: string
example: B
Cust_Age:
type: number
example: 50
City:
type: string
example: Bb
英文:
I have a data frame which is similar to:
df <- data.frame(State = c("A", "B"),
Cust_Age = c(25, 50),
City = c("Aa", "Bb"))
I want the values of each row to be converted into different yaml file.
I expect each file to contain this:
State:
type: string
example: A
Cust_Age:
type: number
example: 25
City:
type: string
example: Aa
Similarly the second yaml file will contain values of second row of data frame.
答案1
得分: 2
你可以使用 library(yaml)
。你需要首先将你的 data.frame
转换成一个列表结构,以便 yaml::as.yaml
能够理解:
library(yaml)
library(purrr)
library(dplyr)
df <- data.frame(State = c("A", "B"),
Cust_Age = c(25, 50),
City = c("Aa", "Bb"))
ys <- df %>%
rowwise() %>%
group_map(function(x, y) {
map(x, ~ list(type = case_when(is.factor(.x) | is.character(.x) ~ "string",
is.numeric(.x) ~ "number",
is.logical(.x) ~ "logical",
TRUE ~ "unknown"),
example = .x)
) %>%
as.yaml()
})
cat(ys[[1L]])
# State:
# type: string
# example: A
# Cust_Age:
# type: number
# example: 25.0
# City:
# type: string
# example: Aa
cat(ys[[2L]])
# State:
# type: string
# example: B
# Cust_Age:
# type: number
# example: 50.0
# City:
# type: string
# example: Bb
如果你期望的数据类型不仅仅是 string
和 number
,请相应地调整你的 case_when
(我添加了 logical
作为一个假设性示例)。
请注意,as.yaml
(正确地)将 25
转换为 25.0
,因为尽管它的外观是整数,但实际上它不是整数。如果你想要 25
,你应该将你的数字声明为整数,添加 L
:
cat(as.yaml(list(example = 25)))
# example: 25.0
cat(as.yaml(list(example = 25L)))
# example: 25
英文:
You can use library(yaml)
. You need to cast your data.frame
to a list structure first, which yaml::as.yaml
can understand:
library(yaml)
library(purrr)
library(dplyr)
df <- data.frame(State = c("A", "B"),
Cust_Age = c(25, 50),
City = c("Aa", "Bb"))
ys <- df %>%
rowwise() %>%
group_map(function(x, y) {
map(x, ~ list(type = case_when(is.factor(.x) | is.character(.x) ~ "string",
is.numeric(.x) ~ "number",
is.logical(.x) ~ "logical",
TRUE ~ "unknown"),
example = .x)
) %>%
as.yaml()
})
cat(ys[[1L]])
# State:
# type: string
# example: A
# Cust_Age:
# type: number
# example: 25.0
# City:
# type: string
# example: Aa
cat(ys[[2L]])
# State:
# type: string
# example: B
# Cust_Age:
# type: number
# example: 50.0
# City:
# type: string
# example: Bb
If you expect other data types than string
and number
adapt your case_when
accordingly (I added logical
as a hypothetical example).
Note that as.yaml
(correctly) transforms 25
to 25.0
because despite its appearance 25
ist not and integer. If you want 25
you should declare your numbers as integers by adding L
:
cat(as.yaml(list(example = 25)))
# example: 25.0
cat(as.yaml(list(example = 25L)))
# example: 25
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论