Utsav Kumar
Utsav Kumar

Reputation: 23

How do i remove dash(-) in R?

I'm very new to r. How should I make r read dash(-) or skip it and calculate average of No. of plant(last column).

Genotype Rep    No. of plant
184  1   8
7    1   7
98   1   -
101  2   7
X    2   8
62   2   -
24   3   3
30   3   4
78   3   8
119  3   8

Upvotes: 1

Views: 4772

Answers (1)

sempervent
sempervent

Reputation: 893

There are several options.

  1. You can use gsub to convert it to an NA. For example gsub('-', NA, dat$'No. of plant', fixed=TRUE). (Use backticks instead of quotes). Then convert the data to numeric using as.numeric()
  2. You can also specify the NA values when importing your data.

Below is an example:

dat=data.frame(Genotype=c(184, 7, 98, 101, 'X'),
           Rep=c(1,1,1,2,2),
           No=c(8,7,'-',7,8))

dat$No <- gsub('-',NA,dat$No,fixed=TRUE)
dat$No <- as.numeric(dat$No)

Upvotes: 1

Related Questions