Q:

Write a R program to convert two columns of a data frame to a named vector

0

Write a R program to convert two columns of a data frame to a named vector.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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"

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now