gpt4 book ai didi

r - 从 R 中的包装文本创建表格

转载 作者:行者123 更新时间:2023-12-01 23:06:52 24 4
gpt4 key购买 nike

编辑:

我想从基于名为 a 的变量的文本中获取一个表格,其中描述单元格将被展开。

a <- 
"
category variable description value
A A This is variable named as A 123
which is responsible for sth
B This is variable named as B 222.1
which is responsible for sth
else
B A This is sth 2
out of 4
Other c Other va This is variable named as 222
ategory riable other variable which can be
nullable
Other va This is variable named as 0
riable A other variable A which can
be nullable
"

我想要的结果:

enter image description here

最佳答案

实现您想要的结果的一个选项是将您的变量作为固定宽度的文件读取,例如使用readr::read_fwf 和我使用 tidyrdplyr 的一些额外的数据整理步骤:

library(dplyr)
library(tidyr)
library(readr)

df <- readr::read_fwf(file = a, skip = 1)
names(df) <- unlist(df[1, ])
df <- df[-1,]
df %>%
filter(!is.na(description)) %>%
tidyr::fill(category, variable) %>%
group_by(category, variable) %>%
summarise(description = paste(description, collapse = " "), value = value[!is.na(value)])
#> `summarise()` has grouped output by 'category'. You can override using the `.groups` argument.
#> # A tibble: 2 × 4
#> # Groups: category [1]
#> category variable description value
#> <chr> <chr> <chr> <chr>
#> 1 A A This is variable named as A which is responsible for … 123
#> 2 A B This is variable named as B which is responsible for … 222.1

关于r - 从 R 中的包装文本创建表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70712360/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com