w3resource

R Programming: Convert given dataframe column(s) to a vector

R Programming: Vector Exercise-16 with Solution

Write a R program to convert given dataframe column(s) to a vector.

Sample Solution :

R Programming Code :

dfc1 = c(1, 2, 3, 4, 5)
dfc2 = c(6, 7, 8, 9, 10)
dfc3 = c(11, 12, 13, 14, 15)
dfc4 = c(16, 17, 18, 19, 20)
v <- data.frame(dfc1=1:5, dfc2=6:10, dfc3=11:15, dfc4=16:20)
print(v)

Sample Output:

dfc1 dfc2 dfc3 dfc4
1    1    6   11   16
2    2    7   12   17
3    3    8   13   18
4    4    9   14   19
5    5   10   15   20                         

R Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a R program to find common elements from multiple vectors.
Next: Write a R program to extract every nth element of a given vector.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.