Joel B
Joel B

Reputation: 103

Turning data frame rows into an R value

I have the following data frame in R:

region_loop | test_regions
------      | ------
field1      | POSTAL_DIST_AB
field2      | POSTAL_DIST_AL
field3      | POSTAL_DIST_B

and I want to turn each row into an individual R value which would appear beneath the data sets on the right hand side of the R interface. NB: the number of rows within my table can change every time the code is run. The values would be:

Value           content of value
field1          "POSTAL_DIST_AB"
field2          "POSTAL_DIST_AL"
field3          "POSTAL_DIST_B"

Upvotes: 0

Views: 63

Answers (2)

Joel B
Joel B

Reputation: 103

I've found a way to do it:

for (i in 1:nrow(df)){
name = paste("fields",i,sep="")
assign(name,df[i,1])
} 

Upvotes: 0

joran
joran

Reputation: 173627

A much better way to do this than using assign would be to create a named vector or list:

setNames(df$test_regions,df$region_loop)

Upvotes: 3

Related Questions