Mutate new variable on one dataframe by deriving value from another dataframe - Incompatible Issue

流过昼夜 提交于 2019-12-11 09:02:27

问题


I am using mutate to create a new column in dataframe A by fetching values from a dataframe B.

I already tried using the below code but it started throwing an error. Not sure whether I am making any mistakes here. Please find the code below.

Apologies, that I can't share data as it is confidential. However the objective is simple and am sure I am making a blunder somewhere. Can you correct me?

Here, dfm is the dataframe which was already created, from which I will use the 'Code' column values for new dataframe(DF) which was also created but without the 'Visit_concept_id' column.

dfm - Old dataframe with 'Code' column of length 256612
DF %>%
   mutate(visit_concept_id = as.integer(as.character(dfm$Code)))

I expect the 'Code' column values from dfm dataframe to be populated in visit_concept_id column of new dataframe(DF) but I am receiving error as shown below. Please help. Not sure how it was working earlier and not now.

Error: Column visit_concept_id must be length 16 (the group size) or one, not 256612

str(df) is given below

Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 256612 obs. of 17 variables: $ visit_occurrence_id : int 1 2 3 4 5 6 7 8 9 10 ... $ person_id : int 127 12097 348097 4324235 214214 4213 41412 634643 241245 3255 ... $ visit_concept_id : int 32036 32036 32036 32036 32036 32036 32036 32036 32036 32036 ... $ visit_start_date : Date, format: "2169-06-08" "2169-06-08" "2169-06-08" ... $ visit_start_datetime : POSIXct, format: "2169-06-08 09:40:00" "2169-06-08 09:41:00" "2169-06-08 09:42:00" ... $ visit_end_date : Date, format: "2169-06-08" "2169-06-08" "2169-06-08" ... $ visit_end_datetime : POSIXct, format: "2169-06-08 09:40:00" "2169-06-08 09:41:00" "2169-06-08 09:42:00" ... $ visit_type_concept_id : int 44818518 44818518 44818518 44818518 44818518 44818518 44818518 44818518 44818518 44818518 ... $ provider_id : int 0 0 0 0 0 0 0 0 0 0 ... $ care_site_id : int 0 0 0 0 0 0 0 0 0 0 ... $ visit_source_value : chr "Outpatient" "Outpatient" "Outpatient" "Outpatient" ... $ visit_source_concept_id : int 0 0 0 0 0 0 0 0 0 0 ... $ admitting_source_concept_id : int 0 0 0 0 0 0 0 0 0 0 ... $ admitting_source_value : chr NA NA NA NA ... $ discharge_to_concept_id : int 0 0 0 0 0 0 0 0 0 0 ... $ discharge_to_source_value : chr NA NA NA NA ... $ preceding_visit_occurrence_id: int 0 1 2 3 4 0 6 7 8 9 ...


回答1:


According to the error message your data is grouped. Try this

DF %>% ungroup() %>%
   mutate(visit_concept_id = as.integer(as.character(dfm$Code)))


来源:https://stackoverflow.com/questions/55395940/mutate-new-variable-on-one-dataframe-by-deriving-value-from-another-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!