I want to match the columnname "Name" from dataframe List to the Rowname of dataframe "Volume" and perform calculation on the columns. Can I use match
or row.match
for this purpose? I am trying to do something like this
Total <- as.data.frame(matrix(0, ncol = 5, nrow = 5))
for (i in 1:nrow(List))
{
match(List$Name, ncol(Volume)), 2])
Value = Total + Volume[i]
print(Value)
}
print(Total)
Problem with the code:
Total <- as.data.frame(matrix(0, ncol = 5, nrow = 5))
# Works fine able to create the desired dataframe
for (i in 1:nrow(List))
# Not able to read element by element in the row. This reads the entire row at one go.
# I want to perform this for all five elements separately
{
match(List$Name, ncol(Volume)), 2])
# This in not the right format. I don't know how to match a row against a column
Value = Total + Volume[i]
# I want to add the data from the matching column to Total
print(Value)
# Print it
}
print(Total)
# In the end print the final value of Total
I want to read the first element in List$Name
match with the corresponding column, extract the column and perform a simple addition on it and store the result in the dataframe "Total". I want the same thing to be done for all the elements in the 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
The actual List dataframe consists of 23 records and the actual Volume dataframe consists of 18000 records.
I am trying to build a function but am not sure as a for loop is necessary for this calculation. Is there a better and easier way to perform this task?