How to filter dataframe with same column name?

ankit kumar

I am reading one text file which has redundant column names.

file.txt

A  B  B  E  E
2  2  4  4  5
3  4  5  6  8

I want to Keep columns which has B and E as column name. but when i read the file

rt<-read.table("file.txt",header=TRUE)

  A B B.1 E E.1
  1 2 4   4 5
  2 4 5   6 8

Can i use regular expression while filtering dataframe?

akrun

We can use grep to select the columns with names that start with either B or E. By default, data.frame do not allow to have to duplicate column names, and it is infact very useful in many ways.

 df1[grep("^(B|E)", names(df1))]
 #  B B.1 E E.1
 #1 2   4 4   5
 #2 4   5 6   8

However, we could read the dataset with check.names=FALSE in the read.table/read.csv, if we need to keep the duplicate column names, but I would not recommend to do that as it will create a lot of confusion while subsetting. Without using check.names, the read.table calls the make.unique to get unique column names even if there are duplicate column names.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to merge pandas DataFrame with same column name?

From Dev

change column names with same name in dataframe

From Dev

How to filter values by Column Name and then extract the rows that have the same value to another CSV file? Python/Pandas

From Dev

Python: I have pandas dataframe which has the same column name. How to change one of those?

From Dev

Python: I have pandas dataframe which has the same column name. How to change one of those?

From Dev

How to filter an excel column by Month name?

From Dev

How filter multiple column with the same condition?

From Dev

pandas, how to filter dataframe by column value

From Dev

how to filter a spark dataframe by a boolean column

From Dev

How to filter a pandas dataframe by dict column?

From Dev

How to add multiple numbers with the same name in the column?

From Dev

How to add multiple numbers with the same name in the column?

From Dev

How to count the same name inside a column?

From Dev

How to get the column name using values in a dataframe?

From Dev

How to name a dataframe column filled by numpy array?

From Dev

How to insert dataframe column name into equation R?

From Dev

How to give a "/" in a column name to a dataframe in R?

From Dev

How to get the column name using values in a dataframe?

From Dev

Read dataframe and when there is a zero find same column name and row name in another dataframe and populate

From Dev

Filter table with column name

From Dev

Multiple Filter in the same column

From Dev

how to name the column same as records of column of another table in oracle

From Dev

How to check if dataframe row name matches column name

From Dev

How to copy one DataFrame column in to another Dataframe if their indexes values are the same

From Dev

How to see if column name exists in a dataframe, and if not create the column with a default value?

From Dev

How should I filter multiple fields with the same name in logstash?

From Dev

Same Table name and Column name

From Dev

How to create independent filter for each column with same condition in excel?

From Dev

Extract distinct values from Dataframe and insert them into new Dataframe with same column Name

Related Related

  1. 1

    How to merge pandas DataFrame with same column name?

  2. 2

    change column names with same name in dataframe

  3. 3

    How to filter values by Column Name and then extract the rows that have the same value to another CSV file? Python/Pandas

  4. 4

    Python: I have pandas dataframe which has the same column name. How to change one of those?

  5. 5

    Python: I have pandas dataframe which has the same column name. How to change one of those?

  6. 6

    How to filter an excel column by Month name?

  7. 7

    How filter multiple column with the same condition?

  8. 8

    pandas, how to filter dataframe by column value

  9. 9

    how to filter a spark dataframe by a boolean column

  10. 10

    How to filter a pandas dataframe by dict column?

  11. 11

    How to add multiple numbers with the same name in the column?

  12. 12

    How to add multiple numbers with the same name in the column?

  13. 13

    How to count the same name inside a column?

  14. 14

    How to get the column name using values in a dataframe?

  15. 15

    How to name a dataframe column filled by numpy array?

  16. 16

    How to insert dataframe column name into equation R?

  17. 17

    How to give a "/" in a column name to a dataframe in R?

  18. 18

    How to get the column name using values in a dataframe?

  19. 19

    Read dataframe and when there is a zero find same column name and row name in another dataframe and populate

  20. 20

    Filter table with column name

  21. 21

    Multiple Filter in the same column

  22. 22

    how to name the column same as records of column of another table in oracle

  23. 23

    How to check if dataframe row name matches column name

  24. 24

    How to copy one DataFrame column in to another Dataframe if their indexes values are the same

  25. 25

    How to see if column name exists in a dataframe, and if not create the column with a default value?

  26. 26

    How should I filter multiple fields with the same name in logstash?

  27. 27

    Same Table name and Column name

  28. 28

    How to create independent filter for each column with same condition in excel?

  29. 29

    Extract distinct values from Dataframe and insert them into new Dataframe with same column Name

HotTag

Archive