Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Combine characters list in R for-loop

I try to combine a list of characters, printed in r by a for-loop, using the function c(), but I can’t do it and cols list stay NULL.

cols <- 
 c(
   for(j in periods) {
     if(is.na(df[grep(j, df$Period),]$Scenario[[1]])) {
       print(j, collapse = ",")
   } else {
     for (k in ssp) {
       print(paste(j, k, sep="_", collapse = ","))
     }
   }  
 }
 )
# Console
[1] "1951-1980"
[1] "1981-2010"
[1] "2011-2040_ssp126"
[1] "2011-2040_ssp370"
[1] "2011-2040_ssp585"
[1] "2041-2070_ssp126"
[1] "2041-2070_ssp370"
[1] "2041-2070_ssp585"
[1] "2071-2100_ssp126"
[1] "2071-2100_ssp370"
[1] "2071-2100_ssp585"
> cols
NULL

I want to ask my loop to do this below automatically:

cols <- c("1951-1980","1981-2010","2011-2040_ssp126","2011-2040_ssp370","2011-2040_ssp585","2041-2070_ssp126","2041-2070_ssp370","2041-2070_ssp585","2071-2100_ssp126","2071-2100_ssp370","2071-2100_ssp585")

How can I ask to my loop to automatically run this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thank you for you help.

To help you,

# My data frame
> df <- map[sample(nrow(map), 30), ]
> dput(df)
df <- structure(list(rowname = c("fr2041-2070_ssp126_5521", "fr2071-2100_ssp370_9962", 
"fr2071-2100_ssp585_6140", "fr2041-2070_ssp126_10362", "fr2071-2100_ssp585_2461", 
"fr2071-2100_ssp370_5668", "fr2011-2040_ssp585_5251", "fr2041-2070_ssp370_7120", 
"fr2071-2100_ssp126_7171", "fr2041-2070_ssp126_6092", "fr2071-2100_ssp370_12266", 
"fr2041-2070_ssp370_4224", "fr2041-2070_ssp370_3195", "fr2071-2100_ssp585_5245", 
"fr2041-2070_ssp126_5228", "fr2071-2100_ssp370_3492", "fr1951-1980_13223", 
"fr2071-2100_ssp585_4505", "fr2041-2070_ssp585_6574", "fr1981-2010_9649", 
"fr1981-2010_8049", "fr2071-2100_ssp370_1269", "fr2041-2070_ssp126_4494", 
"fr2071-2100_ssp370_2894", "fr2071-2100_ssp585_3951", "fr2041-2070_ssp126_3324", 
"fr2071-2100_ssp585_4510", "fr2041-2070_ssp370_7156", "fr2071-2100_ssp585_3029", 
"fr2041-2070_ssp585_2580"), Period = c("2041-2070", "2071-2100", 
"2071-2100", "2041-2070", "2071-2100", "2071-2100", "2011-2040", 
"2041-2070", "2071-2100", "2041-2070", "2071-2100", "2041-2070", 
"2041-2070", "2071-2100", "2041-2070", "2071-2100", "1951-1980", 
"2071-2100", "2041-2070", "1981-2010", "1981-2010", "2071-2100", 
"2041-2070", "2071-2100", "2071-2100", "2041-2070", "2071-2100", 
"2041-2070", "2071-2100", "2041-2070"), Scenario = c("ssp126", 
"ssp370", "ssp585", "ssp126", "ssp585", "ssp370", "ssp585", "ssp370", 
"ssp126", "ssp126", "ssp370", "ssp370", "ssp370", "ssp585", "ssp126", 
"ssp370", NA, "ssp585", "ssp585", NA, NA, "ssp370", "ssp126", 
"ssp370", "ssp585", "ssp126", "ssp585", "ssp370", "ssp585", "ssp585"
)), row.names = c(NA, -30L), class = c("data.table", "data.frame"
))

>Solution :

Here is one option to do this in for loop – print doesn’t have a return value

cols <- character(0)
 for(j in periods) {
     if(is.na(df[grep(j, df$Period),]$Scenario[[1]])) {
       cols <- c(cols, j)
   } else {
     for (k in ssp) {
       cols <- c(cols, paste(j, k, sep="_", collapse = ","))
     }
   }  
 }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading