0

Hello I have 2 dataframes:

df1 looks like:

enter image description here

and the df2 looks like:

enter image description here

I have noticied that the df1 has point symbol (.), while df2 has "-". It is weird because both of them, if I open with a text editor or excel, they have "-".

What I need is to drop all the columns of df1 that match with a value of df2. I have used this:

DataGenSample = df1[,!(names(df1) %in% df2)]
#DataGenSample <- df1[ , !(colnames(df1) %in% df2)] 

but there is no change.

All the Data can be foun here. Whith the code that I have used.

# Data (df1):
DataGen <- read.table("data_CNA.txt",sep="\t", header=TRUE, check.names = FALSE)

# Samples (df2):
DeleteSample <- read.table("MuestrasEliminar.txt",sep="\t", header=TRUE, check.names = FALSE)

#Delete columns:

#DataGenSample = DataGen[,!(names(DataGen) %in% DeleteSample)]
DataGenSample <- DataGen[ , !(colnames(DataGen) %in% DeleteSample)] 

WindSur
  • 141
  • 7

2 Answers2

0

The issue is - vs ..

When you read in the data, your read command probably has an argument like check.names that changes the names to make them "standard" R names - which means no punctuation other than _ and .. If you set check.names = FALSE the original names will be kept, and your code should work just fine.

Edward
  • 10,360
  • 2
  • 11
  • 26
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • hello @Gregor Thomas, I did not know about that command, but it did not work when I've tried to drop the column. – WindSur Apr 17 '20 at 13:14
  • The problem remaining is that `DeleteSample` is a data frame, but you're using it like a vector. Try ` !(colnames(DataGen) %in% DeleteSample$SAMPLE_ID)`, or whatever the relevant column name is. – Gregor Thomas Apr 17 '20 at 18:08
0

Ok, I found out that you need to convert your df in vector first:

vecDeleteSample <- DeleteSample$SAMPLE_ID 

And then you can drop the mathing columns of your vector/list:

DataGenSample <- DataGen[,!(names(DataGen) %in% vecDeleteSample)]
WindSur
  • 141
  • 7