Process one list each element according to corresponding position of other same length list
1
2
Entering edit mode
6.4 years ago
1106518271 ▴ 60

I have two lists, lista and listb,

lista contains three elements, each of their class() is frame:
frame1, frame2, frame3. Each frame nrows = 50, ncols = 100

listb contains three elements, each of their class() is numeric:
vector1, vector2, vector3. Each vector length is 50

I want to do
vector1 replace the 100th col (last column) of frame1,
vector2 replace the 100th col of frame2,
vector3 replace the 100th col of frame3.

How can I realize is not by for cycle use R? Many thanks! I search this question for several hours,but seems no similar can be reference.

yes,for R, it's

lista[[1]][, 100] <- listb[[1]]   
lista[[2]][, 100] <- listb[[2]]  
lista[[3]][, 100] <- listb[[3]]

here 1,2,3 is toy example, what I will process more than 200

R • 1.0k views
ADD COMMENT
1
Entering edit mode

I don't fully understand what you want to do, something like this ?

lista[1][, 100] <- listb[1]
lista[2][, 100] <- listb[2]
lista[3][, 100] <- listb[3]
ADD REPLY
0
Entering edit mode

Can you explain how this is related to bioinformatics? If not it will be closed and it would be better if you try on StackOverflow

ADD REPLY
0
Entering edit mode

Thank you for reminding me. Actually, the last col is coverage value of each position on chromosome, but I should mapping this value to same scale so each sample can be compared when they are visualized. Now is let the transfered value to replace raw value.

ADD REPLY
5
Entering edit mode
6.4 years ago
zx8754 12k

We can use mapply, see this example:

# example data
set.seed(1);lista <- list(frame1 = data.frame(x = letters[1:2], y = runif(2)),
                          frame2 = data.frame(x = letters[3:4], y = runif(2)),
                          frame3 = data.frame(x = letters[5:6], y = runif(2)))

listb <- list(c(1, 11), c(2, 22), c(3, 33))

lista
# $frame1
# x         y
# 1 a 0.2655087
# 2 b 0.3721239
# 
# $frame2
# x         y
# 1 c 0.5728534
# 2 d 0.9082078
# 
# $frame3
# x         y
# 1 e 0.2016819
# 2 f 0.8983897

listb
# [[1]]
# [1]  1 11
# 
# [[2]]
# [1]  2 22
# 
# [[3]]
# [1]  3 33


mapply(function(x, y){
  x[ ncol(x) ] <- y # Assign y to the last column, in your case ncol(x) will return 100.
  x},
  lista, listb, SIMPLIFY = FALSE)
# $frame1
# x  y
# 1 a  1
# 2 b 11
# 
# $frame2
# x  y
# 1 c  2
# 2 d 22
# 
# $frame3
# x  y
# 1 e  3
# 2 f 33
ADD COMMENT

Login before adding your answer.

Traffic: 1639 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6