Question: (Closed) How to match elements of a row to elements of a column in rstudio
0
gravatar for RVRLibra
4.0 years ago by
RVRLibra0
RVRLibra0 wrote:

I want to match the columnname "Name" from dataframe List to the Rowname of dataframe "Volume" and perform calultion on the columns.

Can I use the 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?

rstudio match for R • 1.3k views
ADD COMMENTlink modified 4.0 years ago by Sukhdeep Singh10.0k • written 4.0 years ago by RVRLibra0

Hello RVRLibra!

We believe that this post does not fit the main topic of this site.

This question is better suited at stackoverflow.com. Its a basic programming question.

For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.

If you disagree please tell us why in a reply below, we'll be happy to talk about it.

Cheers!

ADD REPLYlink written 4.0 years ago by Sukhdeep Singh10.0k
Please log in to add an answer.
The thread is closed. No new answers may be added.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 813 users visited in the last hour