What I want to do: I have a data frame where the first column consists of strings, and the rest of the columns are filled with numeric values. I want to add the first three rows to the rest of the rows in groups of three while applying different factors to the rows, like: row1*x + row4*y, row1*x + row5*y, row1*x + row6*y, then row2*x+row4*y, and so on until row3*x+row6*y. Then I want to do the same thing again, but with different values for x and y, and then a third time with different values for x and y. Then I want to do the same with rows 1-3 and 7-9, then the same with 1-3 and 10-12. The exact order is important. I want to write all of this as rows in a new data frame.
I also want to combine the respective strings of the first column, add a marker so I know which summation I made, and then a continuous counter per group. I want to add this to my data frame of results, so I can see which row came from where.
What I have: After extensive juggling with for loops, this code finally does exactly what I want, but it is ugly, and it is reaaaaaally slow with my actual data (actual data frame has 1762 columns).
numbers <- data.frame(replicate(10,sample(1:100,12,rep=TRUE)))
id <- data.frame(id=c("d1","d2","d3","v11","v12","v13","v21","v22","v23","v31","v32","v33"))
data <- cbind(id,numbers)
results <- data.frame()
data.raw <- data[,-1]
legend <- data.frame()
q=1
#################### rows 1-3 with rows 4-6
k=1 # my continuous counter
for(i in 1:3) {
for(j in 4:6){
results <- rbind(results,0.99*(data.raw[i,])+0.01*(data.raw[j,]))
legend[q,1] <- paste(data[i,1],data[j,1],"01",k,sep="_")
q=q+1
k=k+1 }}
k=1
for(i in 1:3){
for(j in 4:6){
results <- rbind(results,0.95*(data.raw[i,])+0.05*(data.raw[j,]))
legend[q,1] <- paste(data[i,1],data[j,1],"05",k,sep="_")
q=q+1
k=k+1 }}
k=1
for(i in (1:3)){
for(j in 4:6){
results <- rbind(results,0.9*(data.raw[i,])+0.1*(data.raw[j,]))
legend[q,1] <- paste(data[i,1],data[j,1],"10",k,sep="_")
q=q+1
k=k+1 }}
#################### rows 1-3 with rows 7-9
k=1
for(i in 1:3){
for(j in 7:9){
results <- rbind(results,0.99*(data.raw[i,])+0.01*(data.raw[j,]))
legend[q,1] <- paste(data[i,1],data[j,1],"01",k,sep="_")
q=q+1
k=k+1 }}
k=1
for(i in 1:3){
for(j in 7:9){
results <- rbind(results,0.95*(data.raw[i,])+0.05*(data.raw[j,]))
legend[q,1] <- paste(data[i,1],data[j,1],"05",k,sep="_")
q=q+1
k=k+1 }}
k=1
for(i in 1:3){
for(j in 7:9){
results <- rbind(results,0.9*(data.raw[i,])+0.1*(data.raw[j,]))
legend[q,1] <- paste(data[i,1],data[j,1],"10",k,sep="_")
q=q+1
k=k+1 }}
#################### rows 1-3 with rows 10-12
k=1
for(i in 1:3){
for(j in 10:12){
results <- rbind(results,0.99*(data.raw[i,])+0.01*(data.raw[j,]))
legend[q,1] <- paste(data[i,1],data[j,1],"01",k,sep="_")
q=q+1
k=k+1 }}
k=1
for(i in 1:3){
for(j in 10:12){
results <- rbind(results,0.95*(data.raw[i,])+0.05*(data.raw[j,]))
legend[q,1] <- paste(data[i,1],data[j,1],"05",k,sep="_")
q=q+1
k=k+1 }}
k=1
for(i in 1:3){
for(j in 10:12){
results <- rbind(results,0.9*(data.raw[i,])+0.1*(data.raw[j,]))
legend[q,1] <- paste(data[i,1],data[j,1],"10",k,sep="_")
q=q+1
k=k+1 }}
mydataframe <- cbind(legend,results)
What I'd like to have: something prettier, shorter, and faster. Apparently it is possible to replace for loops with functions, but here's where I'm lost. I guess I could use something from the apply
family, but I don't completely understand the concept of functions yet, and then it all gets more complicated with the descriptive strings.
Can someone please point me in the right direction?