This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Failure in inner_join

I have two dataframes (df1 and df2) that look like this:

df1:

enter image description here

df2:

enter image description here

My aim is to change the IDs of df2 to geo_accession using df1 as reference:

Example: SYMBOL GSM4187205 ......

But I have encountered two problems:

  1. The number of variables in df1 is 587 (number of samples) while the number of samples in df2 is 552. 35 samples have been removed. How can I fix df1 to be exactly the same as df2?
  2. I have written the following code:
df2.mod <- df2 %>%
  gather(key = "samples", value = "counts", -SYMBOL) %>%
  mutate(samples = gsub("X", "", samples)) %>%
  inner_join(., df1, by = c('samples' = 'title')) %>%
  spread(key = 'geo_accession', value = 'counts') %>%
  column_to_rownames(var = 'SYMBOL')

But it doesn't work because the number of samples and title variables don't match.

Error: <0 rows> (o 0- extensión row.names)

Thank you for your help,

dplyr r

My aim is to change the IDs of df2 to geo_accession using df1 as reference:

Example: SYMBOL GSM4187205 ......

Can you confirm that values in header of dataframe 2 correspond to column 2 of dataframe 1?

Hi,

I confirm. I I manually checked the first 10 with the full dataset. Both files come from the same data: one is the metadata dataset and the other is the gene expression dataset.

1 answer

gsub might be removing more than you need, see example:

id <- c("X123", "X234_A", "X234_X")

gsub("X", "", id)
# [1] "123"   "234_A" "234_" 
gsub("^X", "", id)
# [1] "123"   "234_A" "234_X"

Either fix the gsub line or when you are reading in the data set check.names = FALSE:

df2 <- read.table("myfile.txt", header = TRUE, check.names = FALSE)

Hi,

Adding when I am reading the dataset: check.names = FALSE, it's works! The names come out without the X but, the final result after the "inner_join" is:

[1] SYMBOL        samples       counts        geo_accession    
<0 rows> (o 0- extensión row.names)

I think that it's the number of the variables for name ID: in df2 is 552 and in df1 is 587. How do I know and remove excess variables from df1?

You need to provide reproducible example datasets for us to help you further.

Log in to answer this question.