I am not sure I understand your question, if you want to fill in a data.frame
row by row I would use a matrix.
library(magrittr)
rnames = c("s1","s2","s3")
values = c(c("None", "None", 0, 0 ),
c("site1", "Site 1", 6.943890, -6.0557),
c("site2", "Site 2", 43.943890, -3.055796))
matrix(values,nrow = length(rnames),
ncol = length(values)/length(rnames),
byrow = T) %>% as.data.frame() -> S
colnames(S) <- c("id", "name", "lat", "lng")
S
#> id name lat lng
#> 1 None None 0 0
#> 2 site1 Site 1 6.94389 -6.0557
#> 3 site2 Site 2 43.94389 -3.055796
The other option is to build the dataframe from a a list.
I think this would be more similar to what you would need in a
real-world scenario.
options(stringsAsFactors = FALSE)
df_list <- list(
s1 = data.frame(id = "None", name = "None", lat = 0, lng = 0 ),
s2 = data.frame(id = "site1", name = "Site 1", lat = 6.943890, lng = -6.0557),
s3 = data.frame(id = "site2", name ="Site 2",lat = 43.943890,lng = -3.055796)
)
S = dplyr::bind_rows(df_list)
colnames(S) <- c("id", "name", "lat", "lng")
# i don't think u need this
S$lat <- as.double(S$lat)
S$lng <- as.double(S$lng)
S
#> id name lat lng
#> 1 None None 0.00000 0.000000
#> 2 site1 Site 1 6.94389 -6.055700
#> 3 site2 Site 2 43.94389 -3.055796
As others have said this is not very common in R, for large datasets the list
option will likely be a bit slow. Vectorizing through columns is the
optimal option most of the time.
Created on 2020-04-07 by the reprex package (v0.3.0)