- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to select positive values in an R data frame column?
We know that positive values are greater than
df1$x[which(df1$x>0)]
Example1
set.seed(254) x<−rnorm(20) df1<−data.frame(x) df1
Output
x 1 −0.2581511 2 1.5986312 3 0.1347109 4 −0.4816860 5 1.0202752 6 0.2968755 7 −0.1281462 8 1.6107106 9 −1.9545718 10 0.3993334 11 −0.4458160 12 0.5535872 13 1.6401329 14 −0.2909519 15 −0.1275311 16 0.5795695 17 −0.7438926 18 1.2381494 19 0.3837225 20 0.9689413
Example
df1$x[which(df1$x>=0)]
Output
[1] 1.5986312 0.1347109 1.0202752 0.2968755 1.6107106 0.3993334 0.5535872 [8] 1.6401329 0.5795695 1.2381494 0.3837225 0.9689413
Example2
y<−rnorm(20) df2<−data.frame(y) df2
Output
y 1 1.53798786 2 −0.85463326 3 2.39444451 4 0.82559418 5 −2.22197322 6 −1.04243823 7 −0.04693054 8 −0.68691236 9 −1.63040923 10 −1.42408865 11 1.79834289 12 −0.43978051 13 0.47706345 14 0.59884206 15 −0.69136846 16 0.22906493 17 −0.26332146 18 0.29536361 19 −1.24096107 20 0.04963628
Example
df2$y[which(df2$y>=0)]
Output
[1] 1.53798786 2.39444451 0.82559418 1.79834289 0.47706345 0.59884206 0.22906493 [8] 0.29536361 0.04963628
Example3
z<−sample(−2:2,20,replace=TRUE) df3<−data.frame(z) df3
Output
z 1 −2 2 2 3 0 4 −2 5 −2 6 −2 7 0 8 −2 9 −1 10 −2 11 0 12 −2 13 0 14 2 15 1 16 1 17 1 18 0 19 1 20 −2
Example
df3$z[which(df3$z>=0)]
Output
[1] 2 0 0 0 0 2 1 1 1 0 1
- Related Articles
- How to convert negative values in an R data frame to positive values?
- How to randomly replace values in an R data frame column?
- How to select rows based on range of values of a column in an R data frame?
- How to change row values based on column values in an R data frame?
- How to fill NA values with previous values in an R data frame column?
- How to repeat column values in R data frame by values in another column?
- How to concatenate column values and create a new column in an R data frame?
- How to subset non-duplicate values from an R data frame column?
- How to replace missing values with median in an R data frame column?
- How to select values less than or greater than a specific percentile from an R data frame column?
- How to subtract column values from column means in R data frame?
- How to find the unique values in a column of an R data frame?
- How to find the sum of column values of an R data frame?
- How to replace missing values in a column with corresponding values in other column of an R data frame?
- How to find the groupwise number of positive and negative values in an R data frame?

Advertisements