How to extract specific columns from a DataFrame ?

0 votes

Sample data frame:

df<-as.data.frame(matrix(sample(1:10,100,replace=TRUE),10))
   V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
1   8  9  3  2  4  5  8  8  6   5
2   2  4  5  7  4  6  6  2  4   3
3   1  5  4 10  8 10 10  3  5   2
4  10  7 10  7  2  8  2  3  8  10
5   1  7  4  5  1  3  3  2  1   4
6   4  7  3  4  5  4  1  2  4   5
7  10 10  7  9  5 10  3 10  7   1
8   7  5  2  2  5  6 10 10  4   6
9   3  2 10  2  4  8  3  2  9   7
10  8  6  6  9  9  4  8 10  3   6

Now if I want V2 and V4 ?

data.frame(df$V2,df$V4)

But I want more accurate and compact way!

Apr 11, 2018 in Data Analytics by DataKing99
• 8,130 points
897 views

1 answer to this question.

0 votes

Yes there are so many ways:

df[,c(V2,V4)]

and another way

By using subset()

subset(df,select=c(V2,V4))
answered Apr 11, 2018 by DeepCoder786
• 1,700 points

Related Questions In Data Analytics

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to convert dataframe columns from factors to characters?

To replace all the variables to character ...READ MORE

answered Apr 13, 2018 in Data Analytics by BHARANI
• 400 points
532 views
0 votes
0 answers

How to sort substring from a dataframe

How to sort substring from a dataframe? ...READ MORE

Aug 4 in Data Analytics by anonymous
23 views
0 votes
1 answer

How to convert a text mining termDocumentMatrix into excel or csv in R?

By assuming that all the values are ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,700 points
115 views
0 votes
1 answer

How to create a list of Data frames?

Basically all we have to do is ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,700 points
69 views
0 votes
2 answers

How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

answered Aug 21 in Data Analytics by anonymous
• 25,900 points
120 views
0 votes
1 answer

How to replace NA values in a dataframe with Zero's ?

It is simple and easy: df1<-as.data.frame(matrix(sample(c(NA, 1:10), 100, ...READ MORE

answered Apr 10, 2018 in Data Analytics by CodingByHeart77
• 3,680 points
114 views
0 votes
1 answer

How to spilt a column of a data frame into multiple columns

it is easily achievable by using "stringr" ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,700 points
87 views
0 votes
1 answer

How to save a DataFrame in R??

Suppose your DataFrame is named as df: write.csv(df,file="exmp.csv") Then ...READ MORE

answered Apr 20, 2018 in Data Analytics by DeepCoder786
• 1,700 points
413 views