简体   繁体   中英

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. Each Tibble is named CCLF1_Details-CCLF9_Details. I have columns "COLUMN_LABEL" and "COLUMN_WIDTH" in each tibble. I want to use those columns as parameters for 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

"Error in fwf_widths(width) : (list) object cannot be coerced to type '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?

Was a simple fix. Needed to use sapply instead

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM