dataframe - trouble combining two numeric columns into one R -
so having bit of bother combining 2 columns one. have 2 columns of ages, split child , adolescent columns. example:
child adolescent 1 na 12 2 na 15 3 na 12 4 na 12 5 na 13 6 na 13 7 na 13 8 na 14 9 14 15 10 na 12 11 12 13 12 na 12 13 na 13 14 na 14 15 na 14 16 12 13 17 na 14 18 na 13 19 na 13 20 na 14 21 na 12 22 na 13 23 12 15 24 na 13 25 na 15 26 na 12 27 na 15 28 na 15 29 na 13 30 na 12 31 13 15`
now combine them 1 column called "age" , remove na values. when try following code, encounter problem:
age<- c(na.omit(data$child),na.omit(data$adolescent))
the problem being original data has 514 rows, yet when combine 2 columns, removing nas, somehow end 543 values, not 514 , don't know why.
so, if possible, explain firstly why getting more values planned, , secondly might better way combine 2 columns.
edit: looking this
age 1 12 2 15 3 12 4 12 5 13 6 13 7 13 8 14 9 14 10 12 11 12 12 12 13 13 14 14 15 14 16 12 17 14 18 13 19 13 20 14 21 12 22 13 23 12 24 13 25 15 26 12 27 15 28 15 29 13 30 12 31 13 32 14 33 13 34 11 35 15 36 13
thanks in advance
this line:
age<- c(na.omit(data$child),na.omit(data$adolescent))
concatenates non-missing values child
field non-missing values adolescent
field. suspect want use 1 of these solutions
# youngest age age<- pmin(data$child,data$adolescent,na.rm=t) # oldest age age<- pmax(data$child,data$adolescent,na.rm=t) # child age, replaced adolescent if missing age<- data$child age[is.na(age)] <- data$adolescent[is.na(age)] # ^ notice same logical index ^ # |_______________________________|
Comments
Post a Comment