Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract values from column under given conditions of other column [duplicate]

Tags:

r

I have the following given:

TR  Avg RE
3   0
3   0
4   209.3524872
3   185.6542898
3   0
3   0
3   0
4   136.7522375
4   157.6887675
4   0
3   202.8994858
3   0
3   89.45242983
4   0
3   0
3   218.4987273
3   192.4212849

I want to extract only those values of RE where TR is equal to 4 - how can I do that?

like image 576
Pugl Avatar asked Feb 01 '15 10:02

Pugl


People also ask

How to extract unique values based on two conditions in Excel?

If you want to extract the unique values based on two conditions, here is another array formula can do you a favor, please do as this: 1. Enter the below formula into a blank cell where you want to list the unique values, in this example, I will put it to cell G2, and then press Shift + Ctrl + Enter keys to get the first unique value.

How to select column values based on another Dataframe column value?

You can also select column values based on another DataFrame column value by using DataFrame.loc [] property. The .loc [] property explains how to access a group of rows and columns by label (s) or a boolean array.

How to extract a column of pandas Dataframe based on another value?

You can extract a column of pandas DataFrame based on another value by using the DataFrame.query () method. The query () is used to query the columns of a DataFrame with a boolean expression. The blow example returns a Courses column where the Fee column value matches with 25000.

How to extract cells with unique values from duplicate cells in Excel?

Select Text option from the Formula Type drop down list; Then choose Extract cells with unique values (include the first duplicate) from the Choose a fromula list box; In the right Arguments input section, select a list of cells that you want to extract unique values. 4.


1 Answers

You already have the answer from David's comment. However, for interest below is few additional methods to do it.

Code:

# Method 1:
df[df$TR == 4, "RE"]

# Method 2:
df[ which(df$TR == 4), "RE"]

# Method 3:
subset(df$RE, df$TR == 4)

# Method 4: You could also use the sqldf package to use sql
# install.packages("sqldf")
library(sqldf)
sqldf('select RE from df where TR = 4')
like image 145
Manohar Swamynathan Avatar answered Sep 18 '22 12:09

Manohar Swamynathan