# Error in adding data in columns of R data frame

This is my file

df1 = read.csv("HSC_LSC_BLAST_karyotyope.txt",header = TRUE,sep = ",",row.names = 1)

metadata$$Group <- rep(NA, ncol(df1)) metadata$$Group[seq(1,4,1)] <- 'HSC'
metadata$$Group[seq(5,15,1)] <- 'Blast' metadata$$Group[seq(16,23,1)] <- 'LSC'
#metadata$$Group[seq(13,16,1)] <- 'Mono' metadata$$Cytogenetics[seq(1,4,1)] <- 'Healthy'
metadata$$Cytogenetics[seq(5,21,1)] <- 'NK' metadata$$Cytogenetics[seq(22,23,1)] <- 'Abnormal'

>s metadata$$Cytogenetics[seq(1,4,1)] <- 'Healthy' Error in $$<-.data.frame(*tmp*, Cytogenetics, value = c("Healthy",  :
> replacement has 4 rows, data has 23


Im trying to add other metadata information based on clinical information apart from giving sample labeling , im certainly doing something wrong but im not sure what it is. As my idea is the another column named Cytogenetics which would have respective information status but im getting that error.

I know this is not really an answer, but I strongly discourage you from doing what you do, it's dangerous to impute data manually because it's prone to human mistakes - that you might mess up which columns should carry with metadata.

Actually, I think I have spotted a mistake already. This is your table using the corrected version of your code (with metadata$Cytogenetics <- NA, see Devon's answer): >metadata Group Cytogenetics HSC1 HSC Healthy HSC2 HSC Healthy HSC3 HSC Healthy HSC4 HSC Healthy Blast11 Blast NK Blast12 Blast NK Blast1 Blast NK Blast2 Blast NK Blast3 Blast NK Blast4 Blast NK Blast6 Blast NK Blast7 Blast NK Blast8 Blast NK Blast9 Blast NK LSC1 Blast NK LSC2 LSC NK LSC3 LSC NK LSC4 LSC NK LSC6 LSC NK LSC7 LSC NK LSC8 LSC NK Blast5 LSC Abnormal LSC5 LSC Abnormal  I sort of think that the column LSC1 should be in the LSC group and visa versa the Blast5 should be in Blast group. In this case it seems that it's derived from name, you can actually write a code that will extract the group from the name. df1 = read.csv("HSC_LSC_BLAST_karyotyope.txt", header = TRUE, sep = ",", row.names = 1) metadata <- data.frame(row.names = colnames(df1)) metadata$Group <- sub("[^[:alpha:]]+", "", (colnames(df1)))


However, I don't know how do you know which sample is Healthy, NK or Abnormal. I suppose you have a table where this is written, if it's the case, use that table, don't impute the data manually.

• yes actually the data im taking is realy big so im going for selected sample as you said to do it manually its prone to error ,one more question i want to add respective blood profiling data which are counts such as WBC and other counts, do i have to just make another data frame and add ,or i have to make a column of counts and add it as you shown?
– kcm
Feb 22 '19 at 13:10
• "I don't know how do you know which sample is Healthy, NK or Abnormal. I suppose you have a table where this is written" yes they have categorized based on karyotyping as well as other information
– kcm
Feb 22 '19 at 13:17
• This is not really the good platform for discussing good practices. I would just load the two tables and use function merge to aggregate the data together. Feb 22 '19 at 13:55

Add the Cytogenetics columns and then assign things to a subset of it.

metadata <- data.frame(row.names = colnames(df1))
metadata$$Group <- NA # you don't need to write a vector; just NA does the job metadata$$Group[seq(1,4,1)] <- 'HSC'
metadata$$Group[seq(5,15,1)] <- 'Blast' metadata$$Group[seq(16,23,1)] <- 'LSC'
metadata$$Cytogenetics <- NA # initiate the column with NA values metadata$$Cytogenetics[seq(1,4,1)] <- 'Healthy'
metadata$$Cytogenetics[seq(5,21,1)] <- 'NK' metadata$$Cytogenetics[seq(22,23,1)] <- 'Abnormal'