0

Let's say I have two vectors

x <- c(1, 2, 3, NA, 5, 6, NA, NA, 10, 11, 12, NA, NA, 13)

y <- c(14, 15, 16, 17, 18 ,19, 20, 21, 22, 23, 24, 25, 26 ,27)

They have both length of 14. I want remove NAs from x and the value that corresponds to it in y. And obtain x and y as equal lengths.

for (i in 1:length(x)) {
 for (j in 1:length(y)) {
   if(is.na(x[, i]==y[, j))
   x <- x[-which(x %n% y)]
 }
}

But it doesn't give what I want. Can you please help me ?

RHertel
  • 23,412
  • 5
  • 38
  • 64
Emily565
  • 19
  • 3

2 Answers2

2

I read the comments above, do you want to plot x and y without NAs? If so,you can just plot it,R will not give any warnings or error indication.

x <- c(1, 2, 3, NA, 5, 6, NA, NA, 10, 11, 12, NA, NA, 13)
y <- c(14, 15, 16, 17, 18 ,19, 20, 21, 22, 23, 24, 25, 26 ,27)
plot(x,y)

plot(x,y) does not hava problem. If you need to remove NAs from x and remove the corresponding values in y,you can combine x and y into a data.frame,then use na.omit() to deal with the new dataframe,finally,you can plot it.Ths codes as followed:

df2 <- na.omit(data.frame(x,y))
plot(y~x,df2)

OR:you can use the index to sovle it. is.na is a function help you detect which is NA. Finding the NA position in vector x us is.na(),

then remove the corresponding values in y

last,change the value in x.The code as follows:

y <- y[!is.na(x)]
x <- x[!is.na(x)]

Notice:the order cannot be changed.

chunjin
  • 240
  • 1
  • 8
1
x <- x[!is.na(x)]    
y <- y[!is.na(x)]
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thanks for your answer. I'm using R markdown and asked this question to plot my actual data but it gives error incorrect number of dimensions still. Am I doing anything else wrong? – Emily565 Aug 11 '16 at 11:15
  • That would be difficult to predict based on the given data. This would just remove `NA` values from `x` and corresponding values from `y` – Ronak Shah Aug 11 '16 at 11:18
  • In my actual data, x and y has length of 79. x is a vector with NAs and y is a matrix with 3 columns. I have a selectInput for the column choice. – Emily565 Aug 11 '16 at 11:26
  • so `y` is 79 X 3 matrix ? Can you share the actual `x` and `y` data ? – Ronak Shah Aug 11 '16 at 11:31
  • yes, I have a 79 X 3 matrix. According to the user's column choice( y ), it should plot x and y. x has length of 79 with NAs – Emily565 Aug 11 '16 at 11:37
  • `y <- mat$y[!is.na(x)]` is this what you want? where `mat` is your matrix. – Ronak Shah Aug 11 '16 at 11:40
  • 1
    selectInput("mat", ....., choices = c( a, y, b), .. ) and ı have x as a vector. when the user make choice let's say y, it should remove NA values from x and the value that corresponds to it in y. so the elements of both x and y are decreased from 79. plot(input$y, x, .......) – Emily565 Aug 11 '16 at 11:54
  • If you're trying to remove rows that have NAs in (if you could also combine your data into a single matrix or data frame), are you aware of the `complete.cases` function? – Michael Veale Aug 11 '16 at 11:56
  • 1
    in which variable is the value stored which the user has selected? (either a, y or b) – Ronak Shah Aug 11 '16 at 12:00
  • a, y, b all have NA values in different rows. the one the user selects is the variable that will be eliminated and plotted with x – Emily565 Aug 11 '16 at 12:05
  • Try assigning this value to `y` `y <- input[!is.na(input[mat]), mat]` and then `plot(x, y)` – Ronak Shah Aug 11 '16 at 12:17
  • how can ı rewrite it with not specifying any column like y, like any of them could it be because it is an input from the user. shouldn't ı write it in a for loop ? – Emily565 Aug 11 '16 at 13:43
  • how do you come to know what the user has selected ? (a, b or y) There must be some variable where the value gets stored after the user selects his option. Which is that variable? – Ronak Shah Aug 11 '16 at 13:45
  • okey, thanks a lot – Emily565 Aug 26 '16 at 15:16