-2

I haven't. A clue what I'm doing but

I need to replace NAs in a column in one dataset with info from the column in another dataset

I'm trying to help someone out and that's their problem they need to solve

Has anyone got any idea what the solution might be

  • Welcome to SO. Please take the time to make a reproducible example by including some sample data and preferably what you already tried. https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – VvdL Oct 17 '22 at 14:12

1 Answers1

0

As you haven't provided any of your data, here is a base solution which may or not work for you depending on your situation. Please put more effort into your question if you need something more complicated than this.

df1 <- data.frame(a = c(1, 2, 3, 5, NA, 9))
df2 <- data.frame(a = c(2, 4, 5, 7, 20, 2))
df1$a <- ifelse(is.na(df1$a), df2$a, df1$a)
df1
#>    a
#> 1  1
#> 2  2
#> 3  3
#> 4  5
#> 5 20
#> 6  9
VvdL
  • 2,799
  • 1
  • 3
  • 14
  • 1
    Thank you, I'm not a programmer just trying to help someone, thought posting here would get me further than googling, much appreciated – Patrick Halldorsson Oct 17 '22 at 15:00
  • This is the task: "You will be working with two sets of data. •  The test_with_lost.csv dataset contains information about students study group and their gender. However, study group information was missing for a few students. There are missing values instead of the study group name. •  The lost_info.csv dataset contains missing information about the study group. You need to write the function that finds missing value in one dataset (dataset test_with_lost.csv) and replaces the missing value with the corresponding value from another dataset (lost_info.csv)." My friend sent me this – Patrick Halldorsson Oct 17 '22 at 15:33
  • I converted csv files into excel, but it doesn't really matter. I can send the files. The problem is, I need to match NA's with students' IDs. You cannot just replace NA's wit random info, the second file contains study groups that correspond to students' Ids. – Patrick Halldorsson Oct 17 '22 at 15:35
  • To help you google faster: Use `merge` combined with `all.x = TRUE` or `left_join` from the `dplyr` package to join the data sets by student_id. Then apply the logic to overwrite NA from one data set with the value from te other data set. Look into those functions and I'm sure you will able to complete that task. – VvdL Oct 17 '22 at 15:43