for example:
the 3 lines below results in printing out the sequence as a data frame
dose=seq(0.079,10,0.1)
df_odose <- data.frame(odose=c(dose))
print(df_odose)
Desired is the output ...
looping through a large matrix (my_data1_matrix)
for the 122nd row to be redirected into a data frame.
auc=seq(5,3867,39)
for(row in 122:nrow(my_data1_matrix)) {
for(col in auc) {
print((my_data1_matrix[row, col]))
}
}
FJCC
July 10, 2019, 4:08pm
2
If I understand you correctly, you can use R's subsetting syntax rather than for loops. To get columns 1, 3 and 5 from row 3 to 8 from a matrix into a data frame
MAT <- matrix(1:40, nrow = 8)
MAT
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 1 9 17 25 33
#> [2,] 2 10 18 26 34
#> [3,] 3 11 19 27 35
#> [4,] 4 12 20 28 36
#> [5,] 5 13 21 29 37
#> [6,] 6 14 22 30 38
#> [7,] 7 15 23 31 39
#> [8,] 8 16 24 32 40
col <- c(1,3,5)
df <- as.data.frame(MAT[3:8, col])
df
#> V1 V2 V3
#> 1 3 19 35
#> 2 4 20 36
#> 3 5 21 37
#> 4 6 22 38
#> 5 7 23 39
#> 6 8 24 40
Created on 2019-07-10 by the reprex package (v0.2.1)
1 Like
The requirement for the columns was to loop through the sequence of auc=(5,3867,39). The row remains constant as row 122..
Hence starts with the value at [122,5]
Next would be [122,(5+39)]
Next would be [122,(5+2*39)]
.......
last, 100th, would be [122,3867]
The problem definition is to produce the output for auc as
auc
1 value at row 122, column 5
2 value at row 122, column (5+39)
3 value at row 122, column (5+2*39)
..
100 value at row 122, column (3867)
Just to make sure its clear what I'm looking for.
I've got the auc out output using the loop through the data matrix.
What I'm trying to do is achieve this same output as a data frame.
It seems simple but its got me stumped.
The solution was staring me in the face.
df_test2 = data.frame((my_data1_matrix)) # convert to data frame
Loop over the data frame
auc=seq(5,3867,39)
for(row in 122:nrow(df_test2)) {
for(col in auc) {
print((df_test2[row, col]))
}
}
FJCC
July 10, 2019, 8:10pm
6
If the following does not answer your question, please post your actual code as a Reproducible Example (Reprex)
A minimal reproducible example consists of the following items:
A minimal dataset, necessary to reproduce the issue
The minimal runnable code necessary to reproduce the issue, which can be run
on the given dataset, and including the necessary information on the used packages.
Let's quickly go over each one of these with examples:
Minimal Dataset (Sample Data)
You need to provide a data frame that is small enough to be (reasonably) pasted on a post, but big enough to reproduce your issue.
Let's say, as an example, that you are working with the iris data frame
head(iris)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.…
MAT <- matrix(1:40, nrow = 8)
MAT
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 1 9 17 25 33
#> [2,] 2 10 18 26 34
#> [3,] 3 11 19 27 35
#> [4,] 4 12 20 28 36
#> [5,] 5 13 21 29 37
#> [6,] 6 14 22 30 38
#> [7,] 7 15 23 31 39
#> [8,] 8 16 24 32 40
auc <- seq(1, 5, 2)
#with loop
Vals <- vector("numeric", length = length(auc))
for(i in seq_along(auc)) {
Vals[i] <- MAT[2, auc[i]]
}
df <- as.data.frame(Vals)
df
#> Vals
#> 1 2
#> 2 18
#> 3 34
#without loop
Row2 = MAT[2, auc]
df2 <- as.data.frame(Row2)
df2
#> Row2
#> 1 2
#> 2 18
#> 3 34
Created on 2019-07-10 by the reprex package (v0.2.1)
system
Closed
July 31, 2019, 8:10pm
7
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.