Cannot write sorted rows in informix 12.10 - out-of-memory

When I try to execute a view in an Informix database, I am getting an error message like:
SQL Error [IX000]: Cannot write sorted rows
ISAM error: no free disk space for sort
My system configuration is
OS: Windows server 2019
Ram:32 GB
Informix Version : 12.10
DB Space : 2TB
below adding the view definition.
create view "informix".fan_sensor_view (localcol,tef_1,tef_2,tef_3,tef_4,tef_5,tef_6,tef_7,tef_8,tvf_1,tvf_1a,tvf_2,tvf_2a,tvf_3,tvf_3a,tvf_4,tvf_4a,tvf_5,tvf_6,station,corridor) as
select x0.sti_energy_tef_localcol ,TO_CHAR (round(x0.tef_1_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x0.tef_2_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x0.tef_3_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x0.tef_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'NA' ,TO_CHAR (round(x1.tvf_1_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x1.tvf_2_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x1.tvf_3_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x1.tvf_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'STI' ,'Corridor2' from ("informix".sti_energy_tef x0 join "informix".sti_energy_tvf x1 on (EXTEND (x0.sti_energy_tef_localcol ,year to second) = EXTEND (x1.sti_energy_tvf_localcol ,year to second) ) )
union select x2.sat_energy_tef_localcol ,TO_CHAR (round(x2.tef_1_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x2.tef_2_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x2.tef_3_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x2.tef_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'NA' ,TO_CHAR (round(x3.tvf_1_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x3.tvf_2_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x3.tvf_3_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x3.tvf_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'SAT' ,'Corridor2' from ("informix".sat_energy_tef x2 join "informix".sat_energy_tvf x3 on (EXTEND (x2.sat_energy_tef_localcol ,year to second) = EXTEND (x3.sat_energy_tvf_localcol ,year to second) ) )
union select x4.sae_energy_tef_localcol ,TO_CHAR (round(x4.tef_1_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x4.tef_2_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x4.tef_3_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x4.tef_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'NA' ,TO_CHAR (round(x5.tvf_1_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x5.tvf_2_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x5.tvf_3_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x5.tvf_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'SAE' ,'Corridor2' from ("informix".sae_energy_tef x4 join "informix".sae_energy_tvf x5 on (EXTEND (x4.sae_energy_tef_localcol ,year to second) = EXTEND (x5.sae_energy_tvf_localcol ,year to second) ) )
union select x6.ssn_energy_tef_localcol ,TO_CHAR (round(x6.tef_1_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x6.tef_2_fan_vibrationmm_mm_per_s , 1 ) ),TO_CHAR (round(x6.tef_3_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x6.tef_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'NA' ,TO_CHAR (round(x7.tvf_1_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x7.tvf_2_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x7.tvf_3_fan_vibrationmm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x7.tvf_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'SSN' ,'Corridor2' from ("informix".ssn_energy_tef x6 join "informix".ssn_energy_tvf x7 on (EXTEND (x6.ssn_energy_tef_localcol ,year to second) = EXTEND (x7.ssn_energy_tvf_localcol ,year to second) ) )
union select x8.spc_energy_tef_localcol ,TO_CHAR (round(x8.tef_1_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x8.tef_2_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x8.tef_3_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x8.tef_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'NA' ,TO_CHAR (round(x9.tvf_1_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x9.tvf_2_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x9.tvf_3_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x9.tvf_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'SPC' ,'Corridor2' from ("informix".spc_energy_tef x8 join "informix".spc_energy_tvf x9 on (EXTEND (x8.spc_energy_tef_localcol ,year to second) = EXTEND (x9.spc_energy_tvf_localcol ,year to second) ) )
union select x10.skm_energy_tef_localcol ,TO_CHAR (round(x10.tef_1_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x10.tef_2_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x10.tef_3_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x10.tef_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'NA' ,TO_CHAR (round(x11.tvf_1_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x11.tvf_2_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x11.tvf_3_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x11.tvf_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'SKM' ,'Corridor2' from ("informix".skm_energy_tef x10 join "informix".skm_energy_tvf x11 on (EXTEND (x10.skm_energy_tef_localcol ,year to second) = EXTEND (x11.skm_energy_tvf_localcol ,year to second) ) )
union select x12.snp_energy_tef_localcol ,TO_CHAR (round(x12.tef_1_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x12.tef_2_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x12.tef_3_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x12.tef_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'NA' ,TO_CHAR (round(x13.tvf_1_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x13.tvf_2_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x13.tvf_3_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x13.tvf_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'SNP' ,'Corridor2' from ("informix".snp_energy_tef x12 join "informix".snp_energy_tvf x13 on (EXTEND (x12.snp_energy_tef_localcol ,year to second) = EXTEND (x13.snp_energy_tvf_localcol ,year to second) ) )
union select x14.seg_energy_tef_localcol ,TO_CHAR (round(x14.tef_1_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x14.tef_2_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x14.tef_3_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x14.tef_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'NA' ,TO_CHAR (round(x15.tvf_1_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x15.tvf_2_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x15.tvf_3_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x15.tvf_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'SEG' ,'Corridor2' from ("informix".seg_energy_tef x14 join "informix".seg_energy_tvf x15 on (EXTEND (x14.seg_energy_tef_localcol ,year to second) = EXTEND (x15.seg_energy_tvf_localcol ,year to second) ) )
union select x16.ssa_energy_tef_localcol ,TO_CHAR (round(x16.tef_1_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x16.tef_2_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x16.tef_3_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x16.tef_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'NA' ,TO_CHAR (round(x17.tvf_1_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x17.tvf_2_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x17.tvf_3_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x17.tvf_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'SSA' ,'Corridor1' from ("informix".ssa_energy_tef x16 join "informix".ssa_energy_tvf x17 on (EXTEND (x16.ssa_energy_tef_localcol ,year to second) = EXTEND (x17.ssa_energy_tvf_localcol ,year to second) ) )
union select x18.scr_energy_tef_localcol ,TO_CHAR (round(x18.tef_1_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x18.tef_2_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x18.tef_3_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x18.tef_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'NA' ,TO_CHAR (round(x19.tvf_1_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x19.tvf_2_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x19.tvf_3_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x19.tvf_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'SCR' ,'Corridor1' from ("informix".scr_energy_tef x18 join "informix".scr_energy_tvf x19 on (EXTEND (x18.scr_energy_tef_localcol ,year to second) = EXTEND (x19.scr_energy_tvf_localcol ,year to second) ) )
union select x20.ste_energy_tef_localcol ,TO_CHAR (round(x20.tef_1_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x20.tef_2_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x20.tef_3_fan_vibrationmm_mm_per_s , 1 ) ),TO_CHAR (round(x20.tef_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'NA' ,TO_CHAR (round(x21.tvf_1_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x21.tvf_2_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x21.tvf_3_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x21.tvf_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'STE' ,'Corridor1' from ("informix".ste_energy_tef x20 join "informix".ste_energy_tvf x21 on (EXTEND (x20.ste_energy_tef_localcol ,year to second) = EXTEND (x21.ste_energy_tvf_localcol ,year to second) ) )
union select x22.sgm_energy_tef_localcol ,TO_CHAR (round(x22.tef_1_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x22.tef_2_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x22.tef_3_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x22.tef_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'NA' ,TO_CHAR (round(x23.tvf_1_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x23.tvf_2_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x23.tvf_3_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x23.tvf_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'SGM' ,'Corridor1' from ("informix".sgm_energy_tef x22 join "informix".sgm_energy_tvf x23 on (EXTEND (x22.sgm_energy_tef_localcol ,year to second) = EXTEND (x23.sgm_energy_tvf_localcol ,year to second) ) )
union select x24.stl_energy_tef_localcol ,TO_CHAR (round(x24.tef_1_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x24.tef_2_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x24.tef_3_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x24.tef_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'NA' ,TO_CHAR (round(x25.tvf_1_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x25.tvf_2_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x25.tvf_3_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x25.tvf_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'STL' ,'Corridor1' from ("informix".stl_energy_tef x24 join "informix".stl_energy_tvf x25 on (EXTEND (x24.stl_energy_tef_localcol ,year to second) = EXTEND (x25.stl_energy_tvf_localcol ,year to second) ) )
union select x26.sli_energy_tef_localcol ,TO_CHAR (round(x26.tef_1_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x26.tef_2_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x26.tef_3_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x26.tef_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'NA' ,TO_CHAR (round(x27.tvf_1_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x27.tvf_2_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x27.tvf_3_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x27.tvf_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'SLI' ,'Corridor1' from ("informix".sli_energy_tef x26 join "informix".sli_energy_tvf x27 on (EXTEND (x26.sli_energy_tef_localcol ,year to second) = EXTEND (x27.sli_energy_tvf_localcol ,year to second) ) )
union select x28.sge_energy_tef_localcol ,TO_CHAR (round(x28.tef_1_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x28.tef_2_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x28.tef_3_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x28.tef_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'NA' ,TO_CHAR (round(x29.tvf_1_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x29.tvf_2_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x29.tvf_3_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x29.tvf_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'SGE' ,'Corridor1' from ("informix".sge_energy_tef x28 join "informix".sge_energy_tvf x29 on (EXTEND (x28.sge_energy_tef_localcol ,year to second) = EXTEND (x29.sge_energy_tvf_localcol ,year to second) ) )
union select x30.scc_c1_energy_tef_localcol ,TO_CHAR (round(x30.tef_1_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x30.tef_2_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x30.tef_3_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x30.tef_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'NA' ,TO_CHAR (round(x31.tvf_1_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x31.tvf_2_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x31.tvf_3_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x31.tvf_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'SCC_C1' ,'Corridor1' from ("informix".scc_c1_energy_tef x30 join "informix".scc_c1_energy_tvf x31 on (EXTEND (x30.scc_c1_energy_tef_localcol ,year to second) = EXTEND (x31.scc_c1_energy_tvf_localcol ,year to second) ) )
union select x32.shc_energy_tef_localcol ,TO_CHAR (round(x32.tef_1_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x32.tef_2_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x32.tef_3_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x32.tef_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'NA' ,TO_CHAR (round(x33.tvf_1_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x33.tvf_2_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x33.tvf_3_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x33.tvf_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'SHC' ,'Corridor1' from ("informix".shc_energy_tef x32 join "informix".shc_energy_tvf x33 on (EXTEND (x32.shc_energy_tef_localcol ,year to second) = EXTEND (x33.shc_energy_tvf_localcol ,year to second) ) )
union select x34.swa_energy_tef_localcol ,TO_CHAR (round(x34.tef_1_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x34.tef_2_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x34.tef_3_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x34.tef_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'NA' ,TO_CHAR (round(x35.tvf_1_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x35.tvf_2_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x35.tvf_3_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x35.tvf_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'SWA' ,'Corridor1' from ("informix".swa_energy_tef x34 join "informix".swa_energy_tvf x35 on (EXTEND (x34.swa_energy_tef_localcol ,year to second) = EXTEND (x35.swa_energy_tvf_localcol ,year to second) ) )
union select x36.stc_energy_tvf_localcol ,'NA' ,'NA' ,'NA' ,'NA' ,'NA' ,'NA' ,'NA' ,'NA' ,TO_CHAR (round(x36.tvf_1_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x36.tvf_2_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x36.tvf_3_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x36.tvf_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'STC' ,'Corridor1' from "informix".stc_energy_tvf x36
union select x37.str_energy_tvf_localcol ,'NA' ,'NA' ,'NA' ,'NA' ,'NA' ,'NA' ,'NA' ,'NA' ,TO_CHAR (round(x37.tvf_1_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x37.tvf_2_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x37.tvf_3_fan_vibration_mm_per_s , 1 ) ),'NA' ,TO_CHAR (round(x37.tvf_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'STR' ,'Corridor1' from "informix".str_energy_tvf x37
union select x38.sma_energy_tef_localcol ,TO_CHAR (round(x38.tef_1_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x38.tef_2_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x38.tef_3_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x38.tef_4_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'NA' ,TO_CHAR (round(x39.tvf_1_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x39.tvf_1a_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x39.tvf_2_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x39.tvf_2a_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x39.tvf_3_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x39.tvf_3a_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x39.tvf_4_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x39.tvf_4a_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'SMA' ,'Corridor1' from ("informix".sma_energy_tef x38 join "informix".sma_energy_tvf x39 on (EXTEND (x38.sma_energy_tef_localcol ,year to second) = EXTEND (x39.sma_energy_tvf_localcol ,year to second) ) )
union select x40.scc_c2_energy_tef_localcol ,'NA' ,'NA' ,'NA' ,'NA' ,TO_CHAR (round(x40.tef_5_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x40.tef_6_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x40.tef_7_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x40.tef_8_fan_vibration_mm_per_s , 1 ) ),'NA' ,'NA' ,'NA' ,'NA' ,'NA' ,'NA' ,'NA' ,'NA' ,TO_CHAR (round(x41.tvf_5_fan_vibration_mm_per_s , 1 ) ),TO_CHAR (round(x41.tvf_6_fan_vibration_mm_per_s , 1 ) ),'SCC_C2' ,'Corridor2' from ("informix".scc_c2_energy_tef x40 join "informix".scc_c2_energy_tvf x41 on (EXTEND (x40.scc_c2_energy_tef_localcol ,year to second) = EXTEND (x41.scc_c2_energy_tvf_localcol ,year to second) ) );
Please help me to solve this issue.

Related

Make a bar chart for the multiple columns' mean across groups

I have a data frame like this:
mydf <- data.frame(ID, age, gender, diagnosis, A1, A2, A3)
mydf
ID age gender diagnosis A1 A2 A3
a 22 female 1 4 12 23
b 34 male 2 5 15 33
c 55 female 2 12 46 45
d 55 female 1 45 34 66
e 45 male 1 33 56 32
A1, A2, A3 refer to the questions in my test and the numbers below represent the score that an ID took from that question. "1" and "2" under the diagnosis represent whether the participant has the diagnosis or not.
What I want is to get mean scores for each question and make a bar plot showing the difference across diagnosis groups.
I calculated the mean for question columns like this:
mydf <- rbind(mydf, "mean" = round(colMeans(mydf[,5:7], na.rm = TRUE), 2))
mydf
ID age gender diagnosis A1 A2 A3
a 22.0 female 1 4.0 12.0 23.0
b 34.0 male 2 5.0 15.0 33.0
c 55.0 female 2 12.0 46.0 45.0
d 55.0 female 1 45.0 34.0 66.0
e 45.0 male 1 33.0 56.0 32.0
19.8 32.6 39.8 19.8 32.6 39.8 19.8
So, it added a new row but although I choose only question columns, I don't know why it also gave a mean for Id, age, gender, and diagnosis columns too.
And, I am not sure which steps should I take after this point to make a bar chart for the mean score for each question across diagnoses something like in the picture.
This should work:
library( tidyverse )
### Create tibble
mydf <- tibble( ID = c( "a", "b", "c", "d", "e" ),
age = c( 22, 34, 55, 55, 45 ),
gender = c( "female", "male", "female", "female", "male" ),
diagnosis = c( 1, 2, 2, 1, 1 ),
A1 = c( 4, 5, 12, 45, 33 ),
A2 = c( 12, 15, 46, 34, 56 ),
A3 = c( 23, 33, 45, 66, 32 ) )
### Caclulate the mean for each item within diagnosis group
mydf <- mydf %>% group_by( diagnosis ) %>% mutate( A1_mean = mean( A1, na.rm = T ) ) %>%
mutate( A2_mean = mean( A2, na.rm = T ) ) %>% mutate( A3_mean = mean( A3, na.rm = T ) )
### Reshape from wide to long format
mydf2 <- pivot_longer( mydf, cols = c( A1_mean, A2_mean, A3_mean ), names_to = "question" )
mydf2
### Plot data
ggplot( mydf2, aes( x = factor( question ), y = value, fill = factor( diagnosis ) ) ) +
geom_bar( position = "dodge", stat = "identity" ) + scale_fill_brewer( palette = "Set2" ) +
scale_y_continuous( expand = expansion( mult = c( 0,.05 ) ) ) +
scale_x_discrete( labels = c( "A1_mean" = "A1", "A2_mean" = "A2", "A3_mean" = "A3" ) ) +
theme( legend.position = "bottom" ) + ggtitle( "Mean for each Question by Diagnosis Group" ) +
xlab( "Question" ) + ylab( "Mean" ) + labs( fill = "Diagnosis" )
Thanks a lot to #David!
Inspired by him, this code solved my question.
#choose diagnosis and question columns and calculate the mean score for columns
mydf2 <- mydf %>% select(diagnosis, A1, A2, A3) %>% group_by(diagnosis)%>% summarise_if(is.numeric, mean, na.rm=TRUE)
and this is what I get:
# A tibble: 2 x 4
diagnosis A1 A2 A3
<chr> <dbl> <dbl> <dbl>
1 27.3 34 40.3
2 8.5 30.5 39
and then reshaped the tibble similarly to David's code with tidyverse:
mydf2 <- mydf2 %>%
pivot_longer(!diagnosis, names_to = "Questions", values_to = "Values")
mydf2
# A tibble: 6 x 3
diagnosis Questions Values
<chr> <chr> <dbl>
1 A1 27.3
1 A2 34
1 A3 40.3
2 A1 8.5
2 A2 30.5
2 A3 39
Then made the graph as David showed:
ggplot( mydf2, aes( x = Questions, y = Values, fill = diagnosis ) ) +
geom_bar( position = "dodge", stat = "identity" ) + scale_fill_brewer( palette = "Set2" ) +
scale_y_continuous( expand = expansion( mult = c( 0,.05 ) ) ) +
theme( legend.position = "bottom" ) + ggtitle( "Mean for each Question by Diagnosis Group" ) +
xlab( "Question" ) + ylab( "Mean" ) + labs( fill = "Diagnosis" )
and got the graph I wanted:

Merge multiple columns into one and rename values based on column it came from

Input data.frame looks like this:
col1 col2 col3
1
1
1
1
1
Which can be generated with this: (thanks to #Sotos)
d2 <- data.frame(col1 = c('', 1, '', 1, '', 1),
col2 = c('', '', 1, '', 1, ''),
col3 = c(1, '', '', '', '', ''), stringsAsFactors = FALSE)
And desired output:
col1
3
2
1
2
1
How to merge columns like that? Empty cells are just empty, but solution with NA could be helpful as i can easily fill them.
An approach using data.table
#library( data.table )
DT <- data.table( col1 = c(1,NA, 1, NA, 1),
col2 = c(NA, 1, NA, 1, NA) )
# col1 col2
# 1: 1 NA
# 2: NA 1
# 3: 1 NA
# 4: NA 1
# 5: 1 NA
#update non-NA values to colnumbers
DT[, c("col1", "col2") := as.data.table( ifelse( is.na(DT), NA, col(DT) ) )]
#final output
DT[, .(col1 = fcoalesce( col1, col2 ) ) ][]
# col1
# 1: 1
# 2: 2
# 3: 1
# 4: 2
# 5: 1
update with provided sample data
d2 <- data.frame(col1 = c('', 1, '', 1, '', 1),
col2 = c('', '', 1, '', 1, ''),
col3 = c(1, '', '', '', '', ''), stringsAsFactors = FALSE)
setDT(d2)
cols <- names(d2)
#update values to colunumbers
d2[, (cols) := as.data.table( ifelse( d2 == '', NA, col(d2) ) )]
#final output
d2[, .(col1 = fcoalesce( d2 ) ) ][]
# col1
# 1: 3
# 2: 1
# 3: 2
# 4: 1
# 5: 2
# 6: 1
You can use apply and which like:
apply(d2==1, 1, which)
#[1] 3 1 2 1 2 1
or if needet with unlist
unlist(apply(d2==1, 1, which))
Using the data given by #Sotos.
d2 <- data.frame(col1 = c('', 1, '', 1, '', 1)
, col2 = c('', '', 1, '', 1, '')
, col3 = c(1, '', '', '', '', ''), stringsAsFactors = FALSE)
Here is a tidyverse solution:
df <- df %>%
replace(is.na(.), 0) %>%
mutate(sum = rowSums(.[1:3]))
A vectorized idea is to stack, replace with name index and update the original df, i.e.
d2_a <- subset(stack(d2[-1]), values == 1)
d2_a$values <- gsub('\\D+', '', d2_a$ind)
d2$col1[d2$col1 == ''] <- rev(d2_a$values)
which gives,
col1 col2 col3
1 3 1
2 1
3 2 1
4 1
5 2 1
6 1

joining data.frames from within two lists based on regular expression

This is what my workspace looks like:
list.u = list(list.1 = replicate(n = 10,
expr = {data.frame(Var1 = as.factor(paste0("X", c(1:10))),
Var2 = as.factor(paste0("X", c(11:20))),
value=rnorm(10))},
simplify = F),
list.2 = replicate(n = 10,
expr = {data.frame(Var1 = as.factor(paste0("X", c(1:10))),
Var2 = as.factor(paste0("X", c(11:20))),
value=rnorm(10))},
simplify = F))
list2env(list.u , .GlobalEnv )
names(list.1) <- paste0(LETTERS[1:10],"_NTI")
names(list.2) <- sample(paste0(LETTERS[1:10],"_RC")) # not the same order
###if meaningful can again be possibly converted to
###list.u <- list(list.1, list.2)
What i want to achieve is the joining of two correspondent data.frames based on the string found bevore _NTI and _RC, respectively:
library(dplyr)
df.A <- list.1$A_NTI %>% right_join(list.2$A_RC, by=c("Var1","Var2"))
df.B <- list.1$B_NTI %>% right_join(list.2$B_RC, by=c("Var1","Var2"))
df.C <- list.1$C_NTI %>% right_join(list.2$C_RC, by=c("Var1","Var2"))
and so on for every pair of matching elements of list.1 and list.2
How can i do this`?
You can first match the names using a simple regex, rearrange the data frames in the list, and merge one by one, i.e.
list.1 <- list.1[names(list.1)[match(sub('_.*', '', names(list.1)), sub('_.*', '', names(list.2)))]]
Map(function(i, j)merge(i, j, by = c('Var1', 'Var2'), all.y = TRUE), list.1, list.2)
which gives,
$A_NTI
Var1 Var2 value.x value.y
1 X1 X11 1.111072143 0.9893348
2 X10 X20 0.205016698 -1.0370611
3 X2 X12 -1.153484350 -0.1581219
4 X3 X13 -0.136188465 -0.8258913
5 X4 X14 0.845438616 1.0676754
6 X5 X15 -0.090040790 -0.6626899
7 X6 X16 -0.003032729 0.4220376
8 X7 X17 0.132374562 -0.5993826
9 X8 X18 -0.049654084 0.1161918
10 X9 X19 0.408352891 -0.4193510
$B_NTI
Var1 Var2 value.x value.y
1 X1 X11 -1.54096443 1.6954890
2 X10 X20 0.08418433 -1.1082467
3 X2 X12 0.77535586 0.9035127
4 X3 X13 -1.82040060 0.1870822
5 X4 X14 -1.00129026 -1.6371800
6 X5 X15 0.32455294 0.4544704
7 X6 X16 0.25704291 -0.1451332
8 X7 X17 0.61232730 2.1936744
9 X8 X18 0.43594609 -2.3836932
10 X9 X19 -0.23466536 1.3418739
$C_NTI
Var1 Var2 value.x value.y
1 X1 X11 -0.02400835 0.03265689
2 X10 X20 -1.78936480 1.55964999
....
...
NOTE: The merge(..., all.y = TRUE) is the base R equivalent of dplyr::right_join
stopifnot(length(list.1) == length(list.2))
stopifnot(length(setdiff(substr(names(list.1), 1, 1), substr(names(list.2), 1, 1))) == 0)
Looks like it'll do here to just order each list alphabetical before merging.
Map(merge, list.1[order(names(list.1))], list.2[order(names(list.2))], all.y=TRUE)

sapply for each group using dplyr

df <- data.frame(group = rep(1:4, each = 10),
x1 = rnorm(40), x2 = rnorm(40), x3 = rnorm(40), x4 = rnorm(40),
X5 = rnorm(40), x6 = rnorm(40), x7 = rnorm(40))
sapply(df[, 4:ncol(df)], function(x) sd(x)/mean(x))
I want to apply this function for each group. How do I correct the below command?
df %>% dplyr::group_by(group) %>% do.call(sapply(.[, 4:ncol(.)] function(x) sd(x)/mean(x)))
If I understood your question/objective, the following will give the results you're seeking. It uses the plyr package over the dplyr package. You're likely running into issues using the %>% function with do.call as well, since %>% is just a shortcut for passing the preceding object as the first argument to the subsequent function, and do.call expects a named function as its first argument
library(plyr)
df <- data.frame(group = rep(1:4, each = 10),
x1 = rnorm(40), x2 = rnorm(40), x3 = rnorm(40), x4 = rnorm(40),
X5 = rnorm(40), x6 = rnorm(40), x7 = rnorm(40))
ddply(df,.(group),function(x)
{
sapply(x[,4:ncol(x)],function(y) sd(y)/mean(y))
})
Gives the following results
group x3 x4 X5 x6 x7
1 1 1.650401 -1.591829 1.509770 6.464991 3.520367
2 2 11.491301 -2.326737 -1.725810 -11.712510 2.293093
3 3 -3.623159 -1.416755 2.958689 1.629667 -4.318230
4 4 9.169641 -4.219095 2.083300 1.985500 -1.678107
Consider base R's by (object-oriented wrapper to tapply):
Data (seeded for reproducibility)
set.seed(3219)
df <- data.frame(group = rep(1:4, each = 10),
x1 = rnorm(40), x2 = rnorm(40), x3 = rnorm(40), x4 = rnorm(40),
X5 = rnorm(40), x6 = rnorm(40), x7 = rnorm(40))
by
by_list <- by(df, df$group, function(sub)
sapply(sub[, 4:ncol(sub)], function(x) sd(x)/mean(x))
)
# LIST
by_list
# df$group: 1
# x3 x4 X5 x6 x7
# -1.077354 2.252270 -2.256086 -1.716327 -5.273771
# ------------------------------------------------------------
# df$group: 2
# x3 x4 X5 x6 x7
# 2.580065 5.054094 -10.985927 32.716116 6.732901
# ------------------------------------------------------------
# df$group: 3
# x3 x4 X5 x6 x7
# -3.523565 -1.670539 -5.042595 -7.787303 -15.486737
# ------------------------------------------------------------
# df$group: 4
# x3 x4 X5 x6 x7
# -5.597470 -9.842997 1.985010 33.657188 2.629724
# MATRIX
do.call(rbind, by_list)
# x3 x4 X5 x6 x7
# 1 -1.077354 2.252270 -2.256086 -1.716327 -5.273771
# 2 2.580065 5.054094 -10.985927 32.716116 6.732901
# 3 -3.523565 -1.670539 -5.042595 -7.787303 -15.486737
# 4 -5.597470 -9.842997 1.985010 33.657188 2.629724

how to reshape into multi-level, multi-ordered wide data using dynamic column names

sorry if this has an obvious answer. i'm trying to perform a reshape that has lots of stackoverflow answers when only one column gets used or when the column names can be hardcoded, but i need an answer that will work dynamically when the ordered.cols and unique.cols vectors are not set from the beginning
# these two sets of columns need to be dynamic
# they might be any two sets of columns!
ordered.cols <- c( 'cyl' , 'gear' )
unique.cols <- c( 'am' , 'vs' )
# neither of the above two character vectors will be known beforehand
# so here's the example starting data set
x <- mtcars[ , c( ordered.cols , unique.cols ) ]
# the desired output should have this many records:
unique( x[ , ordered.cols ] )
# but i'm unsure of the smartest way to add the additional columns that i want--
# for *each* unique level in *each* of the variables in
# `unique.cols` there should be one additional column added
# to the final output. then, for that `ordered.cols` combination
# the cell should be populated with the value if it exists
# and NA otherwise
desired.output <-
structure(list(cyl = c(4L, 4L, 4L, 6L, 6L, 6L, 8L, 8L), gear = c(3L,
4L, 5L, 3L, 4L, 5L, 3L, 5L), am1 = c(0L, 0L, 1L, 0L, 0L, 1L,
0L, 1L), am2 = c(NA, 1L, NA, NA, 1L, NA, NA, NA), vs1 = c(1L,
1L, 0L, 1L, 0L, 0L, 0L, 0L), vs2 = c(NA, NA, 1L, NA, 1L, NA,
NA, NA)), .Names = c("cyl", "gear", "am1", "am2", "vs1", "vs2"
), class = "data.frame", row.names = c(NA, -8L))
desired.output
i don't really care if the new columns are named am1, am2, vs1, vs2, or something more convenient. but if there are two distinct values of am in the data, there need to be two data-holding columns in the final output, one of which should be missing if that combination doesn't have the value.
# second example #
ordered.cols <- c( 'a' , 'b' )
unique.cols <- 'd'
# starting data set
y <-
data.frame(
a = c( 1 , 1 , 1 , 2 ) ,
b = c( 1 , 1 , 2 , 2 ) ,
d = c( 'z' , 'y' , 'x' , 'x' )
)
# the desired output here should have this many rows..
unique( y[ , ordered.cols ] )
# now the contents of all columns in `unique.cols`
# (which in this case is only column `d`)
# need to be appended as a widened data set
second.desired.output <-
data.frame(
a = c( 1 , 1 , 2 ) ,
b = c( 1 , 2 , 2 ) ,
d1 = c( 'z' , 'x' , 'x' ) ,
d2 = c( 'y' , NA , NA )
)
second.desired.output
thanks!!!!!!
library(data.table)
reshapeMyData <- function(x, unique.cols, ordered.cols, NA_class="integer") {
DT <- as.data.table(x)
unique.values <- lapply(DT[, unique.cols, with=FALSE], unique)
## If your NA is of the wrong class, it can potentially throw an error,
## depending on when it first shows up. It is better to be explicit about the expected class
NA.classed <- as(NA, NA_class)
### -- This is all one line.. it iterates over the unique combinations of ordered.cols values
DT[, {browser(expr=FALSE)
## These three functions shape the data as needed
setDT(as.list(unlist(
## This mapply call checks if each value is in the given group
mapply(function(v, C) {ifelse(v %in% C, v, NA.classed)}, v=unique.values, C=.SD, SIMPLIFY=FALSE)
)))
}
, keyby=ordered.cols, .SDcols=unique.cols]
} ## // end function reshapeMyData
OUTPUT
reshapeMyData(x, unique.cols, ordered.cols)
cyl gear am1 am2 vs1 vs2
1: 4 3 NA 0 NA 1
2: 4 4 1 0 NA 1
3: 4 5 1 NA 0 1
4: 6 3 NA 0 NA 1
5: 6 4 1 0 0 1
6: 6 5 1 NA 0 NA
7: 8 3 NA 0 0 NA
8: 8 5 1 NA 0 NA
reshapeMyData(y, "d", c("a", "b"), NA_class="character")
a b d1 d2 d3
1: 1 1 z y NA
2: 1 2 NA NA x
3: 2 2 NA NA x
for my purposes, this solution seems to work well:
aggregate( x[ , unique.cols ] , by = x[ , ordered.cols ] , function( w ) paste( sort( unique( w ) ) , collapse = "," ) )
aggregate( y[ , unique.cols ] , by = y[ , ordered.cols ] , function( w ) paste( sort( unique( w ) ) , collapse = "," ) )
sometimes (i am not sure why, but i think it's a factor-coercion issue) nrow( unique( x[ , ordered.cols ] ) ) does not equal the nrow of the output of the above commands. in those cases, this workaround seems to do the trick:
halfway <- aggregate( x[ , unique.cols ] , by = list( apply( x[ , ordered.cols ] , 1 , paste , collapse = "" ) ) , function( w ) paste( sort( unique( w ) ) , collapse = "," ) )
cbind( unique( x[ , ordered.cols ] ) , halfway[ , -1 ] )

Resources