r - How to match elements of row with elements of a column and perform calculations on them in rstudio -
i want match columnname "name" dataframe list rowname of dataframe "volume" , perform calultion on columns.
can use match or row.match purpose.
i trying this
total <- as.data.frame(matrix(0, ncol = 5, nrow = 5)) (i in 1:5) { print(list$s.no[i] * 100) (j in 1:5) { if (match(list$name, colnames(volume)), 2])) value = total + volume[j] print(value) } } print(total)
problem code:
total <- as.data.frame(matrix(0, ncol = 5, nrow = 5)) (i in 1:5) { print(list$s.no[i] * 100) (j in 1:5) { if (match(list$name, colnames(volume)), 2])) # match condition not correct. not know how call column header value = total + volume[i] # want add data matching column total print(value) } } print(total)
i want read first element in list$name match corresponding column, extract column , perform simple addition on , store result in dataframe "total". want same thing done elements in row individually.
list s.no name 2 ba 1 ar 5 ca 3 bl 4 bu volume ar ba bl bu ca -5.1275 1.3465 -1.544 -0.0877 3.2955 -2.2385 1.5065 0.193 1.082 3.074 -5.3705 1.1285 1.966 1.183 -1.9305 -6.4925 1.5735 1.36 -0.0761 2.0875 -5.068 0.9455 0.947 -0.7775 3.832
i know can done after merging 2 files , using sapply function. since wanna use loop on dataframe "list" want compare 2 dataframes , store results separately dataframe everytime after loop over. merging files doesn't solve this.
the actual list dataframe consists of 23 records , actual volume dataframe consists of 18000 records.
i trying build function not sure loop necessary calculation. there better , easier way perform task?
i not sure have understood answer. think have correct code in way:
for (i in 1: length(list$name)) { (j in 1:dim(volume)[2]) { if(list$name[i]==colnames(volume[j])) total[,i]<-total[,i]+volume[,j] }
}
Comments
Post a Comment