R- Count of Specific values and Print in R

Ani

I have created data frame "df"

Age Sex Income
45  Female  3000
25  Female  5000
34  Male    4500

Now I want to count no of females in sex column and Print it like " No of Females = 2" without using any special package

I could see number of male and female while giving code: summary(df$Sex)or table(df$sex)

Tried doing Femdata=df[which(df$Sex =='Female'),] not working sum(df$sex=="Female", na.rm=TRUE) not working df$sex[df$sex=="Female"] not working length(df[df$sex=="Female"]) not working Kindly let me know the solution And also Kindly help me in Printing with some statement and then answer

A5C1D2H2I1M1N2O1R2T1

Generally, you can use cat to print extra information with an answer:

> cat("No of Females = ", nrow(mydf[mydf$Sex == "Female", ]))
No of Females =  2

If you want the result as a character string to use elsewhere, it's probably easier to use sprintf or paste:

> sprintf("No of Females = %s", nrow(mydf[mydf$Sex == "Female", ]))
[1] "No of Females = 2"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to count entries with specific values in R

From Dev

Count specific dates [R]

From Dev

count values in matrix column in R

From Dev

R count subsequent "TRUE" values

From Dev

aggregate and count uniqe values in R

From Dev

Subset columns in R with specific values

From Dev

R - How to select specific values

From Dev

need to count number of specific transitions in a vector in R

From Dev

R count number of specific string in data frame

From Dev

How to count specific differences between columns in R

From Dev

In R, how to print values of fields in a ReferenceClass?

From Dev

In R, how to print values of fields in a ReferenceClass?

From Dev

How to view print_r values

From Dev

Count of unique values in a rolling date range for R

From Dev

R- count values in data.frame

From Dev

match and count values in sequence by group in R

From Dev

How to count and flag unique values in r dataframe

From Dev

Finding count of NA values for combination of columns in R

From Dev

count unique values of multiple columns in R

From Dev

Count number of values followed by another value in r

From Dev

R- count values in data.frame

From Dev

count number of missing values in netcdf file - R

From Dev

count unique values of multiple columns in R

From Dev

How to count unique values in a data frame in R

From Dev

Using R to count number of specific days in a specific month

From Dev

Count values by specific key

From Dev

Count rows for selected column values and remove rows based on count in R

From Dev

Assign values to a specific dimension of array in R

From Dev

Round up values to a specific significant figure in R

Related Related

HotTag

Archive