简体   繁体   中英

R - For Loop through Rows of Dataframe + Write Long Text to File

I am struggling with a particular for loop in R. I have a data frame with 52 rows and about 30 columns. I am writing the for loop to extract data (or values) from each row and to insert them into a long text --- in this case a javascript/geojson code. Currently, the length() won't accept any argument I put into it.

I'm new to R so a lot of this code may be redundant.

Code below:

# start of 'for loop' 
for(row in 1:nrow(intro_df)) { # trying to iterate through each row of the df
  lines <- vector(length()) # trying to deal with getting an error for "replacement has length zero" 
  # start of long text (notice placeholders in sprintf) --- javascript/geojson to be used in VS code later
  lines[row] <- sprintf(" 
{
            \"type\": \"Feature\",
            \"properties\": {
                \"huc8\": %s,
                \"Year\": %i,
                \"Total Water Use\": %f,
                \"Aquaculture\": %f,
                \"Commercial\": %f,
                \"Self-Supplied Domestic\": %f,
                \"Hydroelectric Power\": %f,
                \"Self-Supplied Industrial\": %f,
                \"Irrigation\": %f,
                \"Livestock\": %f,
                \"Mining\": %f,
                \"Public Supply\": %f,
                \"Thermoelectric\": %f,
                \"Total Groundwater\": %f,
                \"Total Surface Water\": %f
            },
            \"geometry\": {
                \"type\": \"Point\",
                \"coordinates\": [%f, %f]
            }
        }, \n", 
  intro_df$huc8[row], # column names where data/values from intro_df should be inserted into sprintf
  intro_df$Year[row], 
  intro_df$Total_WaterUse[row], 
  intro_df$Aquaculture[row], 
  intro_df$Commercial[row], 
  intro_df$Domestic[row], 
  intro_df$Hydroelectric_power[row], 
  intro_df$Industrial[row], 
  intro_df$Irrigation[row], 
  intro_df$Livestock[row], 
  intro_df$Mining[row], 
  intro_df$Public_Supply[row], 
  intro_df$Thermoelectric[row], 
  intro_df$Total_Groundwater[row], 
  intro_df$Total_Surface_Water[row], 
  intro_df$lat[row], 
  intro_df$long[row])

}
all_lines <- paste(lines, collapse = "\n") # store lines to variable all_lines

file_js_points <- file("js_points.txt") # write all_lines to a text file 
writeLines(all_lines, file_js_points)
close(file_js_points)

EDIT I corrected my code to get rid of the 'for loop'. Below is the code that ended up working for me:

# Input data from each row of intro_df into placeholders in sprintf()
lines <- sprintf(" 
{ 
            \"type\": \"Feature\",
            \"properties\": {
                \"huc8\": %s, 
                \"Year\": %s,
                \"Total Water Use\": %s,
                \"Aquaculture\": %s,
                \"Commercial\": %s, 
                \"Self-Supplied Domestic\": %s,
                \"Hydroelectric Power\": %s,
                \"Self-Supplied Industrial\": %s,
                \"Irrigation\": %s, 
                \"Livestock\": %s, 
                \"Mining\": %s, 
                \"Public Supply\": %s, 
                \"Thermoelectric\": %s, 
                \"Total Groundwater\": %s, 
                \"Total Surface Water\": %s 
            }, 
            \"geometry\": { 
                \"type\": \"Point\", 
                \"coordinates\": [%s, %s] 
            } 
        }, \n", 
                      intro_df$huc8, # column names where data/values from intro_df should be inserted into sprintf
                      intro_df$Year, 
                      intro_df$Total_WaterUse, 
                      intro_df$Aquaculture, 
                      intro_df$Commercial, 
                      intro_df$Domestic, 
                      intro_df$Hydroelectric_power, 
                      intro_df$Industrial, 
                      intro_df$Irrigation, 
                      intro_df$Livestock, 
                      intro_df$Mining, 
                      intro_df$Public_Supply, 
                      intro_df$Thermoelectric, 
                      intro_df$Total_Groundwater, 
                      intro_df$Total_Surface_Water, 
                      intro_df$lat, 
                      intro_df$long)

# Collapse all rows into one variable separated by new line 
js_points <- paste(lines, collapse = "\n") 

# Create js_point text file for all js_points
sink(file = "js_points.txt")
cat(js_points)
sink()

The issue is that you are resetting lines to be empty at the start of every loop iteration. You should initialize it (preferably to the correct length, not 0-length) before the loop. And you need to use the argument length = not the function length() . (I also use character so it's the right type of vector.)

lines = character(length = nrow(intro_df))
for(row in 1:nrow(intro_df)) { 
  lines[row] <- ...
}

However , sprintf is vectorized, so you shouldn't need the for loop at all. I use with() to avoid typing the data frame name so many times.

lines <- with(intro_df, sprintf("
  {...

  }",
  huc8,
  Year,
  Total_WaterUse, ...
))

all_lines <- paste(lines, collapse = "\n") # store lines to variable all_lines

file_js_points <- file("js_points.txt") # write all_lines to a text file 
writeLines(all_lines, file_js_points)
close(file_js_points)

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