1

I am very new in R and still learning. My data is the Titanic.csv which has 891 observation and 13 variables. I would like to change the NA value on the 62 observation of PassengerID 62 in column 12 (column_name "Embarked") from NA to "S" and 830 observation to "C".

I found similar postings, but it didn't give me what I need.

How to replace certain values in a specific rows and columns with NA in R?

How to change NA value in a specific row and column?

My assignment is asking to use the below function.

boat<-within(boat,Embarked[is.na(Embarked)]<-"your choice here")

If I do this

boat<-within(boat,Embarked[is.na(Embarked)]<- "S")

or "C" in where it says "your choice here" it replaces both observations with either "S" or "C".

Below is the example of the Titanic.csv file.

PassengerId Survived Pclass Name              Sex    Age  SibSp Parch Ticket Fare    Cabin  Embarked
1           0        3      Braund, Owen      male   22   1     0      A/5   1717.25        S
2           1        1      Cumings,John      female 38   1     0      PC    9971.28  C85   C
17          0        3      Rice, Eugene      male   2    4     1      382   29.125         Q
18          1        2      Williams,Charles  male   0    0            2443  13             S
60          0        3      Goodwin, William  male   11   5     2      CA 21 46.9           S
61          0        3      Sirayanian, Orsen male   22   0     0      2669  7.2292         C
62          1        1      Icard, Amelie     female 38   0     0      11357 80       B28   NA
63          0        1  Harris, Henry         male   45   1     0      36973 83.475   C83   S

My apologies if the sample dataframe is somewhat condensed.

camille
  • 16,432
  • 18
  • 38
  • 60
Marie
  • 11
  • 2
  • I'm guessing the text about deleting git branches is included by accident? – camille Jan 27 '22 at 21:50
  • Hi Camille, thanks for editing my dataframe. No, the text about deleting git branches is not included. Thank you! – Marie Jan 27 '22 at 22:08

1 Answers1

1
# df is you data frame, first one is the row e.g 62, second one is column e.g 12
df[62, 12]

# Now assign "S" with the `<-` operator 

df[62, 12] <- "S"

# and check if NA is changed to S
df[62, 12]

#Embarked
#<chr>   
#  1 S  

# Same with 
df[830, 12] <- "C"
TarJae
  • 72,363
  • 6
  • 19
  • 66
  • 1
    Thank you TarJae for your help and speedy reply! It is working now. – Marie Jan 27 '22 at 22:09
  • To expand on this for a new R user, the brackets after a data object indicate where you want to locate things. The first part (before the comma) indexes rows, the second part (after the comma) indexes columns. So `df[62, 12]` is indexing the row and column. So if you want to change everything in a column or row, you could leave one blank (i.e. `df[,12]` calls all rows in column 12, and df[62,] calls all columns in row 12. You could also call multiple specific rows/columns, i.e. `df[c(1,6,12), c(3,5,9)]` would call observations 1, 6, and 9 and columns 3, 5, and 9. Good luck! – jpsmith Jan 27 '22 at 22:14