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,250 points
8,692 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,720 points

Related Questions In Data Analytics

0 votes
1 answer

How to extract specific columns from a dataframe in R?

Hi@akhtar, You can use the select method to ...READ MORE

answered Aug 7, 2020 in Data Analytics by MD
• 95,460 points
1,989 views
0 votes
1 answer
0 votes
1 answer
+1 vote
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
• 420 points
19,572 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,720 points
1,963 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,720 points
1,397 views
+1 vote
2 answers

How to sort a data frame by columns in R?

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

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
1,839 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,750 points
2,081 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,720 points
1,816 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,720 points
5,203 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP