简体   繁体   English

R代码:如何从小标题列表中提取一列并将其用作函数的参数?

[英]R code: How can I extract a column from a list of tibbles and use it as a parameter for functions?

I have a list of 9 tibbles called CCLF_Details. 我有一个9个小标题CCLF_Details的列表。 Each Tibble is named CCLF1_Details-CCLF9_Details. 每个Tibble都命名为CCLF1_Details-CCLF9_Details。 I have columns "COLUMN_LABEL" and "COLUMN_WIDTH" in each tibble. 我在每个小标题中都有“ COLUMN_LABEL”和“ COLUMN_WIDTH”列。 I want to use those columns as parameters for read_fwf. 我想将这些列用作read_fwf的参数。

So far I've done 到目前为止,我已经完成了

width <- lapply(CCLF_details, "[","COLUMN_WIDTH", drop = FALSE)
label <- lapply(CCLF_details, "[","COLUMN_WIDTH", drop = FALSE)

but when I run it through read_fwf, I get 但是当我通过read_fwf运行它时,我得到了

"Error in fwf_widths(width) : (list) object cannot be coerced to type 'double'" “ fwf_widths(width)中的错误:(列表)对象无法强制输入“ double”类型”

When inspecting "width", it states it is a list of tibbles with one column (and that column is numeric) instead of a list of numeric vectors. 当检查“宽度”时,它指出它是一个带有一列(并且该列是数字)的小标题列表,而不是数字矢量列表。

How can I get the columns in a format that I can run the list as a parameter for a Map function? 如何以一种格式将列用作Map函数的参数来获取列?

Was a simple fix. 是一个简单的修复。 Needed to use sapply instead 需要使用sapply代替

width <- sapply(CCLF_details, "[","COLUMN_WIDTH", drop = FALSE)
label <- sapply(CCLF_details, "[","COLUMN_WIDTH", drop = FALSE)

Then could use it as arguments for read_fwf by using 然后可以通过使用它作为read_fwf的参数

fwf_widths(widths = as.vector(width), col_names = as.vector(label))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从循环中的函数列表中提取参数 - How to extract a parameter from a list of functions in a loop 如何将 table() 与 dplyr group by、来自 purrr 的映射和数据帧/tibbles 列表一起使用? (在 R 中) - How to use table() with dplyr group by, map from purrr and a list of dataframes/tibbles)? (In R) 如何使用 purrr 在 R 的深度嵌套列表列中提取属性? - How can I use purrr to extract an attribute in a deeply nested list column in R? 在 R 中使用 sapply function 时,如何维护小标题的名称列表? - How can I maintain the list of names of the tibbles when using sapply function in R? 如何将两个小标题或数据帧中的值粘贴到 R 中的组合显示表中? - How can I paste values from two tibbles or dataframes together into a combined display table in R? R中的小标题列表中的命名矢量列表 - List of named vector from list of tibbles in R 我如何在 R 中将 lapply 生成的 tibbles 作为单独的 dataframe - How I can get the tibbles generated by lapply as a seperate dataframe in R 如何将两个小标题乘以列名 - How can I multiply two tibbles by column names 小标题中的列表列:我可以将一个列表列与另一个列表列链接吗? - List-columns in tibbles: Can I link a list-column with another list-column? R:从列表中提取参数 - R: Extract parameter from list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM