last day (14 days later) » 

12:09 PM
1
A: looping through data.frames

amwill04You are not assigning the variables correctly in the loop. Below is a sample loop that will correctly go over each row assigning the variable. e.g. the first loop i == 1, note I have changed how the column name is generated. for(i in 1:nrow(cont)){ vehicle <- cont[i, 3] perturb <- ...

 
Thanks a bundle. It worked.. I was wondering how this thing is working: In some cases the col_name <- paste(cont[i, 5], cont[i, 6], sep = '_') had the same name for 2 instances and this code handled it by giving names "metformin_0.00001" and "metformin_0.00001.1". Can you explain why and how it happened?
 
You could try creating an empty vector with col_names <- c() and then within the loop col_names <- c(col_names, paste(cont[i, 5], cont[i, 6], sep = '_')) obviously remove the other instance of col_names in the loop. and then after the loop and after the div <- div[,-1] assign the column names via colnames(div) <- col_names
 
Ok, thanks. Can you tell me what will be the possible solution for the situation where perturb contains more than 1 columns and I want to take perturb = sum of columns / no.of columns and then divide control / perturb
 
Depends whether or not you know how many columns it is going to be. if that varies then you are going to probably want to write a function the deal with that. The function above deals with a known number of columns. That detail aside, in the df[,which(colnames(df) == x)] you can use the OR operator | so that it becomes df[,which(colnames(df) == x | colnames(df) == y)], you could even wrap that in the sum()/nrow() functions to get the value out. However that will give you a single value, which I'm guessing is the point as you want the mean.
 
Now in every case I have to take 5 columns for vehicle (that I'm doing by: vehicle <- cont[i, 5:9]), sum their values and divide them by 5: It will be the vehicle (that I'm doing by: tmp <- sum(expression[,which(colnames(expression) == vehicle)])/5) but it is not working. @amwill04
I've modified my data.frame cont, so that you can see the change, plus I have modified the code according to my results but it's not working (I've also posted that in question, for your better understanding). Thanks
 
12:09 PM
You need to create a vector of columns, so vehicle <- cont[i, 5:9]) should be vehicle <- cont[i, c(5:9)]), note the c() in the column selection. Following that the simplest way will be to change your which()` statement from ==`` to %in%. This is due to vehicle <- cont[i, c(5:9)]) will give a vector of characters, i.e. and the statement will select all columns where the column name appears "IN" that vector, i.e. [1] "GSM118912" "GSM118911". Therefor it will appear as: vehicle <- cont[i, c(5:9)]) tmp <- sum(expression[,which(colnames(expression) %in% vehicle)])/5)
 
tmp <- sum(expression[,which(colnames(expression) %in% vehicle)])/5 I am trying this line (please note last ) is removed) but it is giving the following error: Error in FUN(X[[i]], ...) : only defined on a data frame with all numeric variables
 
Sorry this became too long for the forum to moved to chat.

You must have non-numeric values in one of the columns you are trying to sum up. Do you have NA's?
 
No, I don't have NA's
 
For a test tryin sum(..., na.rm = TRUE), where ... is the previous sum
 
tmp <- sum(expression[,which(colnames(expression) %in% vehicle)], na.rm=TRUE)/5
I tried this but still the error is same..
 
12:20 PM
if you were to run tmp <- expression/5, will it work? There is something wrong with how the data is stored.
Also, if you use RStudio click the little blue arrow on expression on the environment window and make sure all the columns are either num or int
R has a nasty habit of importing as factors.
 
All the columns in expression are "num".
tmp <- expression/5 => I didn't get this..!
I tried this: tmp <- expression[,which(colnames(expression) %in% vehicle)]/5
And the error is: Error in data.frame(value, row.names = rn, check.names = FALSE, check.rows = FALSE) :
row names supplied are of the wrong length
 
basically tmp <- expression/5 will divide every value in your expression dataframe by 5 i assigned it to tmp so as not to change expression.
 
What if I take out the values for each column separately then add them and then divide by 5... ?
 
forget about vehicle for the time being.
I dont follow..?
 
vehicle <- cont[i, 5]
vehicle2 <- cont[i, 6]
vehicle3 <- cont[i, 7]
vehicle4 <- cont[i, 8]
vehicle5 <- cont[i, 9]
tmp <- expression[,which(colnames(expression) == vehicle)]
tmp2 <- expression[,which(colnames(expression) == vehicle2)]
tmp3 <- expression[,which(colnames(expression) == vehicle3)]
tmp4 <- expression[,which(colnames(expression) == vehicle4)]
tmp5 <- expression[,which(colnames(expression) == vehicle5)]
tmp_new <- sum(tmp,tmp1,tmp2,tmp3,tmp4,tmp5)/5
tmp2 <- expression[,which(colnames(expression) == perturb)]
final_tmp <- tmp_new/tmp2
Is it possible..?
Apparently it worked without any error but final output is like this: > head(div)[1:4,1:2]
estradiol_0.00000001 estradiol_0.00000001.1
1007_s_at Inf Inf
1053_at Inf Inf
117_at Inf Inf
121_at Inf Inf
why is this Inf...? In some columns there are values and in some there is Inf...!
> head(tmp_new)
[1] 18178617
I think this "sum" function is not rightly applied, it could be "merge" instead: to add every value in each row of these (tmp, tmp2... tmp5) and then divide by 5. What do you think..?
> class(tmp)
[1] "numeric"
> typeof(tmp)
[1] "double"
 
12:53 PM
ok, but that will give the error somewhere.
 
> length(of every tmp) is same = 22268. It means columns are extracted in correct way and the problem is to sum them up or merge them..
there was no error.
 
sum will obviously give you a single value. It is summing everything within that vector.
 
yes, so what is the way around this..?
 
when you set i = 1 what is the values of tmp to tmp5?
oh i think i now know what you want.
tmp <- expression[,which(colnames(expression) %in% vehicle)]
row_tots <- as.data.frame(rowSums(tmp))
row_tots <- row_tots/5
tmp <- row_tots/expression[,which(colnames(expression) == perturb)]
I think thats what you want.
 
Yes, it looks right but still the first column of my output file only contains "Inf"
One more thing, I don't know how I can set i = 1.. will it be like: for(i in 1:2(cont)){ ??
 
1:04 PM
does the expression[,which(colnames(expression) == perturb)] give you 0's?
 
No, there will be a numerical value every time
 
well yeah but 0 is numerical and dividing by 0 will give you infinity
 
No, there is no 0 value in whole expression file.
 
also when creating a function in the form for (i in ...) {...} you want to then test that function line by line, you do that by simply give i a value e.g. i <- 1. so you can then run through the function and see where errors occur.
ok try the following:
i <- 2
div <- data.frame(row.names(expression))
vehicle <- cont[i, 3]
perturb <- cont[i, 4]
col_name <- paste(cont[i, 5], cont[i, 6], sep = '_')

tmp <- expression[,which(colnames(expression) == vehicle)]/
expression[,which(colnames(expression) == perturb)]

div <- cbind(div, tmp)

colnames(div)[i + 1] <- col_name
View(div)
edit
colnames(div)[2] <- col_name
 
Yes, it is working fine.
 
1:14 PM
so that doesnt produce the INF?
now do the last parts
div <- div[,-1]
row.names(div) <- row.names(expression)
 
No, it contains values in decimal..
 
and check that there is no inf values
 
> row.names(div) <- row.names(expression)
Error in `rownames<-`(x, value) :
attempt to set 'rownames' on an object with no dimensions
 
sorry its because it was a two column dataframe that has one column removed. R then changes it to a vectot
to get around that simple run div <- data.frame(test = div)
dont worry about the column name for the time being
now you can assign row names to the data frame
 
Yes, it works..
 
1:19 PM
and no inf?
 
No inf.. All is well
 
ok re un everything with i <- nrow(cont)
it may be the last one that is causing the issue.
sorry, its rather difficult without the data in front of me
 
I am getting a error pop up from R studio "Unable to establish connection with R session"
Can I ask your email address, so that I can send you the input files and you can see it yourself..? Thank you very much for your time.
Ok, that popup is closed. yes the code executed correctly.
 
email is amwilliams89@icloud.com, I may have to look at them later this afternoon if that is alright. Got some work I need to catch up on.
 
Sure, when you get enough time.
 
1:26 PM
send it over with a screen shot of your errors and a transcript of you R input and I will try and replicate the errors and fix
 
One of the input file is 24.4 MB, I hope that will not a problem to send over email..? I have already sent the email. Did you receive it?
 
if you use dropbox send a download link instead, be easier.
and no I havent received any thing
 
1:46 PM
received it
 
Ok, take your time and let me know when you finish doing it. Many thanks for your help.
 
2:00 PM
have replied, only needed an extra line
 
2:37 PM
Yeap, I checked.. It's working fine.. Many thanks for your time.. Have a good day..
 

last day (14 days later) »