I can't test this without your files, but I think this should work. You use map_dfc to iterate a function over a sequence of integers from 1 to 30, using sprintf to create the string for the filename from the integer at each step. map_dfc specifies that you want the results of the iterating to be bound by column into a new data frame.
library(dplyr)
library(purrr)
SUN2015 <- map_dfc(seq(30), function(i) {
require(oce)
require(ncdf4)
filename <- sprintf("C:/images/gertmo/figure_sun%s.nc", x)
vart <- nc_open(filename)
ray2 <- ncvar_get(vart,"degrees")
nc_close(vart)
final <- ray[c(ocean)]
return(final)
})
In base R, you could do this with lapply and then cbind.
SUN2015_list <- lapply(seq(30), function(i) {
require(oce)
require(ncdf4)
filename <- sprintf("C:/images/gertmo/figure_sun%s.nc", x)
vart <- nc_open(filename)
ray2 <- ncvar_get(vart,"degrees")
nc_close(vart)
final <- ray[c(ocean)]
return(final)
})
SUN2015 <- do.call(cbind, SUN2015_list)