Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a logical vector in R and use which function

Tags:

r

I have a dataset with three columns. The first column is type, second column is area and third column is worth. I want to write a logical vector such that the type =1 , area = 3 and worth = 6. I was able to create the data frame using subset but I couldn't create a logical vector.

hello <- read.csv("type.csv")
hello1 <- subset(hello, type==1 & area ==3 & worth ==6)

There are many NA values in worth column. The data set is https://www.dropbox.com/s/gjjwmnr8uxmy18y/type.csv

Thanks.

Jdbaba

like image 240
Jd Baba Avatar asked Feb 02 '13 05:02

Jd Baba


People also ask

How do you define a logical vector in R?

A logical vector is a vector that only contains TRUE and FALSE values. In R, true values are designated with TRUE, and false values with FALSE. When you index a vector with a logical vector, R will return values of the vector for which the indexing vector is TRUE.

What is logical function R?

logical() function in R Language is used to check whether a value is logical or not. Syntax: is.logical(x) Parameters: x: Value to be checked.


1 Answers

So the question remains answered:

which(with(hello, type == 1 & area == 3 & Worth == 6))

Remember, you can just use it as:

which(hello$type1 == 1 & hello$area == 3 & hello$Worth == 6)

as well. However, when you have more statements to check for, a with comes in handy as it allows you to check without typing hello$ every time.

like image 193
Arun Avatar answered Oct 02 '22 17:10

Arun