Write a R program to convert two columns of a data frame to a named vector.
df = data.frame(code = c("R","G","W","B"), name = c("Red", "Green", "White", "Black") ) print("Original vector:") print(df) result = setNames(as.character(df$name), df$code) print(result)
Sample Output:
[1] "Original vector:" code name 1 R Red 2 G Green 3 W White 4 B Black R G W B "Red" "Green" "White" "Black"
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
[1] "Original vector:" code name 1 R Red 2 G Green 3 W White 4 B Black R G W B "Red" "Green" "White" "Black"need an explanation for this answer? contact us directly to get an explanation for this answer