I want to a go through 'paragraphs' a list of however many paragraphs and save each individual one as a new variable and tried writing this code block and am getting the following errors:
for (value in paragraphs) {
nam <- paste("p", value, sep = ".")
value <- assign(nam, 1:value)
}
Error in 1:value : NA/NaN argument
In addition: Warning message:
In assign(nam, 1:value) : NAs introduced by coercion
Any advice as to where to go from here?
I think you are trying to create a number of variables called p1
, p2
, p3
etc, each with the corresponding paragraph written to it. In that case, the following should work:
for (value in seq_along(paragraphs)) {
nam <- paste("p", value, sep = ".")
value <- assign(nam, paragraphs[value])
}
However, it is not a great idea to write a bunch of variables to the global workspace, and it would be better if the paragraphs were all in a named list. You say they are in a list already, but it's not clear if you mean a an actual R list or just a vector. If they are in a list already, you could do:
p <- setNames(paragraphs, paste0("p", seq_along(paragraphs)))
This would allow you to access each paragraph as p$p1
, p$p2
etc.
If the paragraphs are in a vector rather than a list, you could do
p <- setNames(as.list(paragraphs), paste0("p", seq_along(paragraphs)))
to get the same result.
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.