X3DOM TwoSidedMaterial tag not working - xhtml

I try to give blue color outside of the face and green color inside of the same face. I added this tag.
<TwoSidedMaterial diffuseColor="0 0 1" shininess="0.0" backDiffuseColor="0 1 0" backShininess="0.0" separateBackColor="true" backTransparency="0.0"></TwoSidedMaterial>
diffuseColor= Blue and backDiffuseColor=Green but it shows only blue color on both side (inside and outside). backDiffuseColor not applyed.
This is a refrence link

There might be a bug in the library because the reference example also shows a single material (red) instead of two (red and blue):
<x3d>
<scene>
<transform rotation='-1 0 0 -1.571' translation='0 13.03 0'>
<shape>
<appearance>
<TwoSidedMaterial diffuseColor="1 0 0" shininess="0.0" backDiffuseColor="0 0 1" backShininess="0.0" separateBackColor="true" backTransparency="0.0"></TwoSidedMaterial>
</appearance>
<indexedFaceSet DEF='Plane002-3-FACES' normalIndex='92 76 65 -1 56 65 76 -1 65 56 30 -1 120 30 56 -1 30 120 103 -1 69 103 120 -1 103 69 85 -1 13 85 69 -1 85 13 33 -1 20 33 13 -1 33 20 83 -1 46 83 20 -1 83 46 102 -1 89 102 46 -1 102 89 60 -1 6 60 89 -1 60 6 105 -1 113 105 6 -1 105 113 136 -1 122 136 113 -1 136 122 3 -1 138 3 122 -1 3 138 81 -1 36 81 138 -1 81 36 125 -1 72 125 36 -1 125 72 90 -1 26 90 72 -1 90 26 7 -1 35 7 26 -1 7 35 39 -1 116 39 35 -1 39 116 49 -1 137 49 116 -1 49 137 61 -1 53 61 137 -1 61 53 24 -1 112 24 53 -1 24 112 98 -1 66 98 112 -1 98 66 77 -1 4 77 66 -1 77 4 28 -1 11 28 4 -1 28 11 75 -1 37 75 11 -1 75 37 93 -1 80 93 37 -1 93 80 55 -1 1 55 80 -1 55 1 47 -1 54 47 1 -1 47 54 70 -1 58 70 54 -1 70 58 82 -1 73 82 58 -1 82 73 27 -1 117 27 73 -1 27 117 62 -1 19 62 117 -1 62 19 32 -1 107 32 19 -1 32 107 87 -1 114 87 107 -1 87 114 119 -1 57 119 114 -1 119 57 109 -1 21 109 57 -1 41 92 52 -1 65 52 92 -1 52 65 22 -1 30 22 65 -1 22 30 104 -1 103 104 30 -1 104 103 135 -1 85 135 103 -1 135 85 97 -1 33 97 85 -1 97 33 17 -1 83 17 33 -1 17 83 5 -1 102 5 83 -1 5 102 115 -1 60 115 102 -1 115 60 129 -1 105 129 60 -1 129 105 25 -1 136 25 105 -1 25 136 10 -1 3 10 136 -1 10 3 100 -1 81 100 3 -1 100 81 14 -1 125 14 81 -1 14 125 29 -1 90 29 125 -1 29 90 95 -1 7 95 90 -1 95 7 134 -1 39 134 7 -1 134 39 118 -1 49 118 39 -1 118 49 44 -1 61 44 49 -1 44 61 15 -1 24 15 61 -1 15 24 99 -1 98 99 24 -1 99 98 131 -1 77 131 98 -1 131 77 88 -1 28 88 77 -1 88 28 9 -1 75 9 28 -1 9 75 0 -1 93 0 75 -1 0 93 111 -1 55 111 93 -1 111 55 63 -1 47 63 55 -1 63 47 106 -1 70 106 47 -1 106 70 91 -1 82 91 70 -1 91 82 40 -1 27 40 82 -1 40 27 94 -1 62 94 27 -1 94 62 110 -1 32 110 62 -1 110 32 34 -1 87 34 32 -1 34 87 68 -1 119 68 87 -1 68 119 101 -1 109 101 119 -1 130 41 132 -1 52 132 41 -1 132 52 48 -1 22 48 52 -1 48 22 38 -1 104 38 22 -1 38 104 86 -1 135 86 104 -1 86 135 50 -1 97 50 135 -1 50 97 8 -1 17 8 97 -1 8 17 133 -1 5 133 17 -1 133 5 128 -1 115 128 5 -1 128 115 67 -1 129 67 115 -1 67 129 16 -1 25 16 129 -1 16 25 108 -1 10 108 25 -1 108 10 79 -1 100 79 10 -1 79 100 71 -1 14 71 100 -1 71 14 139 -1 29 139 14 -1 139 29 45 -1 95 45 29 -1 45 95 123 -1 134 123 95 -1 123 134 64 -1 118 64 134 -1 64 118 124 -1 44 124 118 -1 124 44 42 -1 15 42 44 -1 42 15 31 -1 99 31 15 -1 31 99 78 -1 131 78 99 -1 78 131 43 -1 88 43 131 -1 43 88 2 -1 9 2 88 -1 2 9 127 -1 0 127 9 -1 127 0 121 -1 111 121 0 -1 121 111 12 -1 63 12 111 -1 12 63 96 -1 106 96 63 -1 96 106 51 -1 91 51 106 -1 51 91 23 -1 40 23 91 -1 23 40 18 -1 94 18 40 -1 18 94 74 -1 110 74 94 -1 74 110 126 -1 34 126 110 -1 126 34 59 -1 68 59 34 -1 59 68 84 -1 101 84 68 -1' coordIndex='35 0 36 -1 1 36 0 -1 36 1 37 -1 2 37 1 -1 37 2 38 -1 3 38 2 -1 38 3 39 -1 4 39 3 -1 39 4 40 -1 5 40 4 -1 40 5 41 -1 6 41 5 -1 41 6 42 -1 7 42 6 -1 42 7 43 -1 8 43 7 -1 43 8 44 -1 9 44 8 -1 44 9 45 -1 10 45 9 -1 45 10 46 -1 11 46 10 -1 46 11 47 -1 12 47 11 -1 47 12 48 -1 13 48 12 -1 48 13 49 -1 14 49 13 -1 49 14 50 -1 15 50 14 -1 50 15 51 -1 16 51 15 -1 51 16 52 -1 17 52 16 -1 52 17 53 -1 18 53 17 -1 53 18 54 -1 19 54 18 -1 54 19 55 -1 20 55 19 -1 55 20 56 -1 21 56 20 -1 56 21 57 -1 22 57 21 -1 57 22 58 -1 23 58 22 -1 58 23 59 -1 24 59 23 -1 59 24 60 -1 25 60 24 -1 60 25 61 -1 26 61 25 -1 61 26 62 -1 27 62 26 -1 62 27 63 -1 28 63 27 -1 63 28 64 -1 29 64 28 -1 64 29 65 -1 30 65 29 -1 65 30 66 -1 31 66 30 -1 66 31 67 -1 32 67 31 -1 67 32 68 -1 33 68 32 -1 68 33 69 -1 34 69 33 -1 70 35 71 -1 36 71 35 -1 71 36 72 -1 37 72 36 -1 72 37 73 -1 38 73 37 -1 73 38 74 -1 39 74 38 -1 74 39 75 -1 40 75 39 -1 75 40 76 -1 41 76 40 -1 76 41 77 -1 42 77 41 -1 77 42 78 -1 43 78 42 -1 78 43 79 -1 44 79 43 -1 79 44 80 -1 45 80 44 -1 80 45 81 -1 46 81 45 -1 81 46 82 -1 47 82 46 -1 82 47 83 -1 48 83 47 -1 83 48 84 -1 49 84 48 -1 84 49 85 -1 50 85 49 -1 85 50 86 -1 51 86 50 -1 86 51 87 -1 52 87 51 -1 87 52 88 -1 53 88 52 -1 88 53 89 -1 54 89 53 -1 89 54 90 -1 55 90 54 -1 90 55 91 -1 56 91 55 -1 91 56 92 -1 57 92 56 -1 92 57 93 -1 58 93 57 -1 93 58 94 -1 59 94 58 -1 94 59 95 -1 60 95 59 -1 95 60 96 -1 61 96 60 -1 96 61 97 -1 62 97 61 -1 97 62 98 -1 63 98 62 -1 98 63 99 -1 64 99 63 -1 99 64 100 -1 65 100 64 -1 100 65 101 -1 66 101 65 -1 101 66 102 -1 67 102 66 -1 102 67 103 -1 68 103 67 -1 103 68 104 -1 69 104 68 -1 105 70 106 -1 71 106 70 -1 106 71 107 -1 72 107 71 -1 107 72 108 -1 73 108 72 -1 108 73 109 -1 74 109 73 -1 109 74 110 -1 75 110 74 -1 110 75 111 -1 76 111 75 -1 111 76 112 -1 77 112 76 -1 112 77 113 -1 78 113 77 -1 113 78 114 -1 79 114 78 -1 114 79 115 -1 80 115 79 -1 115 80 116 -1 81 116 80 -1 116 81 117 -1 82 117 81 -1 117 82 118 -1 83 118 82 -1 118 83 119 -1 84 119 83 -1 119 84 120 -1 85 120 84 -1 120 85 121 -1 86 121 85 -1 121 86 122 -1 87 122 86 -1 122 87 123 -1 88 123 87 -1 123 88 124 -1 89 124 88 -1 124 89 125 -1 90 125 89 -1 125 90 126 -1 91 126 90 -1 126 91 127 -1 92 127 91 -1 127 92 128 -1 93 128 92 -1 128 93 129 -1 94 129 93 -1 129 94 130 -1 95 130 94 -1 130 95 131 -1 96 131 95 -1 131 96 132 -1 97 132 96 -1 132 97 133 -1 98 133 97 -1 133 98 134 -1 99 134 98 -1 134 99 135 -1 100 135 99 -1 135 100 136 -1 101 136 100 -1 136 101 137 -1 102 137 101 -1 137 102 138 -1 103 138 102 -1 138 103 139 -1 104 139 103 -1'>
<coordinate DEF='Plane002-3-COORD' point='-50.42 0 -9.972 -47.45 -1.832 -9.803 -44.48 -3.602 -9.299 -41.52 -5.25 -8.479 -38.55 -6.718 -7.37 -35.59 -7.958 -6.01 -32.62 -8.927 -4.445 -29.66 -9.592 -2.729 -26.69 -9.93 -0.9201 -23.72 -9.93 0.9201 -20.76 -9.592 2.729 -17.79 -8.927 4.445 -14.83 -7.958 6.01 -11.86 -6.718 7.37 -8.897 -5.25 8.479 -5.931 -3.602 9.299 -2.966 -1.832 9.803 0 0 9.972 2.966 1.832 9.803 5.931 3.602 9.299 8.897 5.25 8.479 11.86 6.718 7.37 14.83 7.958 6.01 17.79 8.927 4.445 20.76 9.592 2.729 23.72 9.93 0.9201 26.69 9.93 -0.9201 29.66 9.592 -2.729 32.62 8.927 -4.445 35.59 7.958 -6.01 38.55 6.718 -7.37 41.52 5.25 -8.479 44.48 3.602 -9.299 47.45 1.832 -9.803 50.42 0 -9.972 -50.42 0 -3.324 -47.45 -0.6108 -3.268 -44.48 -1.201 -3.1 -41.52 -1.75 -2.826 -38.55 -2.239 -2.457 -35.59 -2.653 -2.003 -32.62 -2.976 -1.482 -29.66 -3.197 -0.9097 -26.69 -3.31 -0.3067 -23.72 -3.31 0.3067 -20.76 -3.197 0.9097 -17.79 -2.976 1.482 -14.83 -2.653 2.003 -11.86 -2.239 2.457 -8.897 -1.75 2.826 -5.931 -1.201 3.1 -2.966 -0.6108 3.268 0 0 3.324 2.966 0.6108 3.268 5.931 1.201 3.1 8.897 1.75 2.826 11.86 2.239 2.457 14.83 2.653 2.003 17.79 2.976 1.482 20.76 3.197 0.9097 23.72 3.31 0.3067 26.69 3.31 -0.3067 29.66 3.197 -0.9097 32.62 2.976 -1.482 35.59 2.653 -2.003 38.55 2.239 -2.457 41.52 1.75 -2.826 44.48 1.201 -3.1 47.45 0.6108 -3.268 50.42 0 -3.324 -50.42 0 3.324 -47.45 0.6108 3.268 -44.48 1.201 3.1 -41.52 1.75 2.826 -38.55 2.239 2.457 -35.59 2.653 2.003 -32.62 2.976 1.482 -29.66 3.197 0.9097 -26.69 3.31 0.3067 -23.72 3.31 -0.3067 -20.76 3.197 -0.9097 -17.79 2.976 -1.482 -14.83 2.653 -2.003 -11.86 2.239 -2.457 -8.897 1.75 -2.826 -5.931 1.201 -3.1 -2.966 0.6108 -3.268 0 0 -3.324 2.966 -0.6108 -3.268 5.931 -1.201 -3.1 8.897 -1.75 -2.826 11.86 -2.239 -2.457 14.83 -2.653 -2.003 17.79 -2.976 -1.482 20.76 -3.197 -0.9097 23.72 -3.31 -0.3067 26.69 -3.31 0.3067 29.66 -3.197 0.9097 32.62 -2.976 1.482 35.59 -2.653 2.003 38.55 -2.239 2.457 41.52 -1.75 2.826 44.48 -1.201 3.1 47.45 -0.6108 3.267 50.42 0 3.324 -50.42 0 9.972 -47.45 1.832 9.803 -44.48 3.602 9.299 -41.52 5.25 8.479 -38.55 6.718 7.37 -35.59 7.958 6.01 -32.62 8.927 4.445 -29.66 9.592 2.729 -26.69 9.93 0.9201 -23.72 9.93 -0.9201 -20.76 9.592 -2.729 -17.79 8.927 -4.445 -14.83 7.958 -6.01 -11.86 6.718 -7.37 -8.897 5.25 -8.479 -5.931 3.602 -9.299 -2.966 1.832 -9.803 0 0 -9.972 2.966 -1.832 -9.803 5.931 -3.602 -9.299 8.897 -5.25 -8.479 11.86 -6.718 -7.37 14.83 -7.958 -6.01 17.79 -8.927 -4.445 20.76 -9.592 -2.729 23.72 -9.93 -0.9201 26.69 -9.93 0.9201 29.66 -9.592 2.729 32.62 -8.927 4.445 35.59 -7.958 6.01 38.55 -6.718 7.37 41.52 -5.25 8.479 44.48 -3.602 9.299 47.45 -1.832 9.803 50.42 0 9.972'></coordinate>
<normal vector='0.2028 0.2688 -0.9415 -0.4858 0.0228 -0.8737 0.4858 0.4405 -0.7548 -0.2028 0.4372 0.8761 -0.4858 0.6055 -0.6303 0.2028 -0.2688 0.9415 -0.4858 -0.0228 0.8737 -0.2028 0.9134 0.3529 0.4858 -0.4405 0.7548 0.2028 0.4372 -0.8761 0.2028 0.4356 0.8769 -0.4858 0.4793 -0.7308 0.4858 -0.0228 -0.8737 -0.4858 -0.6055 0.6303 0.2028 0.723 0.6603 0.2028 0.9134 -0.3529 0.4858 0.1829 0.8546 0.2028 -0.4372 0.8761 0.4858 -0.6055 -0.6303 -0.4858 -0.6834 -0.5447 -0.4858 -0.4793 0.7308 -0.5256 -0.8507 0 0.2028 -0.9134 0.3529 0.4858 -0.4793 -0.7308 -0.2028 0.9127 -0.3545 0.2028 0.2671 0.942 -0.4858 0.7719 0.4098 -0.2028 -0.5908 -0.7808 -0.2028 0.5893 -0.7819 0.2028 0.832 0.5162 -0.2028 -0.9127 0.3545 0.4858 0.7719 -0.4098 -0.2028 -0.833 -0.5147 -0.2028 -0.5893 0.7819 0.2028 -0.9127 -0.3545 -0.4858 0.8341 0.261 -0.4858 0.5717 0.661 -0.4858 0.3369 -0.8064 0.4858 -0.7719 0.4098 -0.2028 0.9627 0.179 0.2028 -0.5893 -0.7819 0.255 -0.9647 0.0647 0.4858 0.8341 -0.261 0.4858 0.5717 -0.661 0.2028 0.9627 -0.179 0.4858 0.7923 0.369 -0.4858 -0.3369 0.8064 -0.2028 -0.0912 -0.9749 0.4858 -0.8341 0.261 -0.2028 0.9792 -0.0008 0.4858 -0.5717 0.661 0.4858 -0.3369 -0.8064 0.2028 -0.9627 0.179 -0.4858 0.8466 -0.2171 -0.4858 -0.138 -0.863 -0.2028 0.0894 -0.9751 -0.4858 -0.8466 0.2171 -0.4858 -0.8678 -0.1033 -0.4858 -0.2943 -0.8229 0.4858 -0.8466 -0.2171 -0.2028 -0.0894 0.9751 -0.2028 0.9623 -0.1808 -0.2028 -0.7242 -0.659 0.2028 -0.0894 -0.9751 0.4858 0.8721 0.0578 -0.2028 -0.9623 0.1808 -0.4858 0.711 -0.5083 0.4858 0.0228 0.8737 0.2028 -0.9623 -0.1808 -0.4858 -0.711 0.5083 -0.2028 -0.2688 -0.9415 0.4858 0.6055 0.6303 -0.4858 0.6834 0.5447 -0.4858 -0.4405 -0.7548 0.4858 -0.711 -0.5083 -0.2028 0.4356 -0.8769 -0.4436 -0.8887 0.1152 -0.2028 0.723 -0.6603 0.4858 0.6834 -0.5447 0.4858 0.4793 0.7308 -0.4858 0.1829 -0.8546 -0.2028 0.5908 0.7808 -0.2028 -0.4372 -0.8761 -0.2028 -0.4356 0.8769 0.4436 -0.8887 -0.1152 -0.2028 -0.723 0.6603 0.4858 -0.6834 0.5447 -0.2028 -0.9134 -0.3529 0.2028 0.5908 -0.7808 -0.4858 -0.1829 0.8546 -0.2028 0.833 0.5147 0.2028 -0.4356 -0.8769 -0.149 -0.9865 0.0664 -0.2028 0.2671 -0.942 0.2028 -0.723 -0.6603 0.2028 0.9127 0.3545 0.4858 -0.1829 -0.8546 0.2028 -0.5908 0.7808 -0.2028 0.832 -0.5162 0.2028 0.833 -0.5147 0.2028 0.5893 0.7819 0.149 -0.9865 -0.0664 -0.2028 -0.2671 0.942 -0.2028 -0.832 0.5162 0.2028 -0.833 0.5147 -0.2028 0.0912 0.9749 0.2028 -0.2671 -0.942 -0.4858 -0.7719 -0.4098 0.4858 0.3369 0.8064 -0.255 -0.9647 -0.0647 0.2028 -0.832 -0.5162 0.2028 0.0912 -0.9749 -0.4858 0.7923 -0.369 -0.4858 0.138 0.863 -0.4858 -0.8341 -0.261 0.2028 -0.0912 0.9749 -0.4858 0.8678 0.1033 -0.4858 -0.5717 -0.661 0.2028 0.9792 0.0008 -0.2028 -0.9627 -0.179 -0.4858 -0.7923 0.369 0.4858 0.138 -0.863 -0.4858 0.2943 0.8229 0.4858 0.8466 0.2171 0.4858 0.8678 -0.1033 -0.2028 0.7242 0.659 0.4858 -0.7923 -0.369 0.4858 0.2943 -0.8229 0.4858 -0.138 0.863 0.2028 0.0894 0.9751 0.5256 -0.8507 0 0.2028 0.7242 -0.659 0.4858 -0.8678 0.1033 0.4858 -0.2943 0.8229 0.2028 0.9623 0.1808 0.2028 -0.7242 0.659 -0.2028 0.2688 0.9415 -0.4858 0.8721 -0.0578 -0.4858 0.4405 0.7548 0.4858 0.711 0.5083'></normal>
</indexedFaceSet>
</shape>
</transform>
...
</scene>
</x3d>
Your best bet is probably to open an issue in the X3DOM repository on Github.

Related

GAMs in R: Fewer unique covariate combinations than df

I tried fitting gams to some dataframes I have. All minus one work. It fails with the error:
Error in smooth.construct.tp.smooth.spec(object, dk$data, dk$knots) : A term has fewer unique covariate combinations than specified maximum degrees of freedom
I looked a bit on the internet but couldn't really figure out what's really going wrong. All my 7 over dataframes run without a problem.
I then ran epiR::epi.cp(srtm[-c(1,7,8)]) and it gave me this output:
$cov.pattern
id n curv_plan curv_prof dem slope ca
1 1 1 1.113192e-02 3.991046e-03 3909 43.601479 5.225853
2 2 1 -2.686749e-03 3.474989e-03 3312 35.022511 4.418310
3 3 1 -1.033450e-02 -4.626922e-03 3326 36.678623 4.421465
4 4 1 -5.439283e-03 2.066148e-03 4069 31.501045 3.887526
5 5 1 -2.602015e-03 -1.249511e-04 3021 37.199219 5.010560
6 6 1 1.068216e-03 1.216902e-03 2844 44.694374 4.852220
7 7 1 -1.855443e-02 -5.965539e-03 2841 42.753750 5.088554
8 8 1 2.363193e-03 2.353357e-03 2833 33.160995 4.652209
9 9 1 2.169674e-02 1.049735e-02 2964 32.311535 4.671970
10 10 1 2.850910e-02 9.416230e-03 2956 50.791847 3.496096
11 11 1 -1.932028e-02 4.949751e-04 2794 38.714302 4.217102
12 12 1 -1.372750e-03 -4.437230e-03 3799 48.356312 4.597039
13 13 1 1.154181e-04 -4.114155e-03 3808 54.669777 3.518823
14 14 1 2.743768e-02 7.829833e-03 3580 23.674162 3.268744
15 15 1 7.216539e-03 9.818082e-04 3969 29.421440 4.354250
16 16 1 2.385139e-03 6.333927e-04 3635 10.555381 4.905733
17 17 1 -1.129411e-02 2.719948e-03 2805 29.195084 4.807369
18 18 1 4.584329e-04 -1.497223e-03 3676 32.754879 3.729304
19 19 1 1.883965e-03 4.189690e-03 3165 30.973505 4.833158
20 20 1 -5.350136e-03 -2.615470e-03 2745 32.534698 4.420852
21 21 1 1.484253e-02 -1.245213e-03 3872 26.113234 4.045357
22 22 1 -2.449377e-02 -5.045668e-04 2931 31.060991 5.170872
23 23 1 -2.962795e-02 -9.271557e-03 2917 21.680889 4.547461
24 24 1 -2.487545e-02 -7.834328e-03 2736 41.775677 4.543325
25 25 1 2.890568e-03 -2.040353e-03 2577 47.003765 3.739546
26 26 1 -5.119631e-03 8.869720e-03 3401 38.519680 5.428564
27 27 1 6.171266e-03 -6.515175e-04 2687 36.678623 4.152842
28 28 1 -8.297552e-03 -7.053435e-03 3678 39.532673 4.081311
29 29 1 8.652663e-03 2.394378e-03 3515 33.895370 4.220177
30 30 1 -2.528805e-03 -1.293259e-03 3404 42.548138 4.266330
31 31 1 1.899994e-02 6.367806e-03 3191 41.696201 3.300749
32 32 1 -2.243623e-02 -1.866033e-04 2433 34.162479 5.364681
33 33 1 -6.934012e-03 9.280805e-03 2309 32.667160 5.650699
34 34 1 -1.121149e-02 6.376335e-05 2188 31.119059 4.706416
35 35 1 -1.429000e-02 5.299596e-04 2511 34.543365 4.538456
36 36 1 -7.168889e-03 1.301791e-03 2625 30.826660 4.059711
37 37 1 -4.226461e-03 7.440552e-03 2830 33.398251 4.941027
38 38 1 -2.635832e-03 8.748529e-03 3378 45.972672 4.861779
39 39 1 -2.007920e-02 -8.081778e-03 3281 31.735376 5.173269
40 40 1 -3.453595e-02 -6.867430e-03 2690 47.515182 4.935358
41 41 1 1.698363e-03 -8.296107e-03 2529 42.224693 4.386349
42 42 1 5.257193e-03 1.021242e-02 2571 43.070564 4.194372
43 43 1 6.968817e-03 5.538784e-03 2581 36.055031 4.209373
44 44 1 -7.632907e-04 2.803704e-04 2582 28.257311 4.230427
45 45 1 -3.468894e-03 -9.099842e-04 2409 29.421440 4.190946
46 46 1 1.879089e-02 6.532978e-03 3733 41.535984 4.032614
47 47 1 -1.076225e-03 -1.138945e-03 2712 39.260731 4.580621
48 48 1 -5.306205e-03 2.667941e-03 3446 34.250553 4.925404
49 49 1 -5.380515e-03 -2.595619e-03 3785 50.561493 4.642792
50 50 1 -2.571232e-03 -2.063937e-03 3768 46.160892 4.728879
51 51 1 -7.638110e-03 -2.432463e-03 3413 32.401161 5.058373
52 52 1 -2.950254e-03 -2.034031e-04 3852 32.543564 4.443869
53 53 1 -2.702386e-03 -1.776183e-03 2483 31.002720 3.879390
54 54 1 -3.892425e-02 -2.266178e-03 2225 26.126318 5.750985
55 55 1 -2.644659e-03 3.034660e-03 2192 32.103516 4.949506
56 56 1 -2.862503e-02 3.673996e-04 2361 23.930893 5.181818
57 57 1 6.263880e-03 -7.725377e-04 3780 17.752790 4.890797
58 58 1 1.054093e-03 -1.563014e-03 3089 36.422310 4.520845
59 59 1 9.474340e-04 -3.901043e-03 3155 42.552841 4.265886
60 60 1 5.569567e-03 -1.770366e-04 3516 13.166321 4.772187
61 61 1 -8.342760e-03 -9.908290e-03 3097 36.815479 5.346615
62 62 1 -1.422498e-03 -1.645628e-03 2865 29.802414 4.131463
63 63 1 4.523963e-02 1.067406e-02 2163 36.154739 3.369432
64 64 1 -1.164162e-02 6.808200e-04 2316 19.610609 4.634536
65 65 1 -8.043590e-03 9.395104e-03 2614 44.298817 3.983136
66 66 1 -1.925332e-02 -4.521391e-03 2035 31.205780 4.134195
67 67 1 -1.429050e-02 5.435983e-03 2799 38.876656 4.180761
68 68 1 6.935605e-04 3.015038e-03 2679 37.863647 4.213497
69 69 1 -5.062089e-03 5.961242e-04 2831 32.401161 3.729215
70 70 1 -3.617065e-04 -2.874465e-03 3152 45.871994 4.703659
71 71 1 -4.216370e-02 -4.917050e-03 3726 25.376934 4.614913
72 72 1 -2.184333e-02 -2.840071e-03 3610 43.138550 4.237120
73 73 1 -1.735273e-02 -2.199261e-03 3339 33.984894 4.811754
74 74 1 1.929157e-02 5.358084e-03 3447 32.356407 3.355368
75 75 1 -4.118797e-02 -2.408211e-03 3251 22.373844 5.160147
76 76 1 -1.393304e-02 7.900328e-05 3297 22.090260 4.724728
77 77 1 -3.078095e-02 -5.535597e-03 3143 37.298687 4.625203
78 78 1 1.717030e-02 -1.120720e-03 3617 37.965389 4.627342
79 79 1 -5.965119e-04 -5.377157e-04 3689 28.360373 4.767213
80 80 1 7.843294e-03 -9.579902e-04 3676 48.356312 3.907819
81 81 1 5.994634e-03 2.034169e-03 2759 25.142431 3.980591
82 82 1 -1.323012e-02 2.393529e-03 3972 26.880308 5.107575
83 83 1 6.312347e-03 2.877600e-04 3323 32.167103 3.496723
84 84 1 -1.180464e-02 4.438243e-03 3790 40.369972 4.081389
85 85 1 -8.333334e-03 4.009274e-03 3248 14.931417 4.881107
86 86 1 2.016023e-03 -5.707344e-04 3994 18.305449 4.278613
87 87 1 -5.515654e-03 -8.373593e-04 3368 40.703190 4.229169
88 88 1 8.931696e-03 1.677515e-03 4651 30.133842 4.327270
89 89 1 1.962347e-04 -7.458636e-04 5075 57.352509 3.263017
90 90 1 -2.880805e-02 -5.200595e-04 2645 11.976726 5.634262
91 91 1 -2.101875e-02 -5.110677e-03 3109 34.218582 4.925558
92 92 1 -8.390786e-03 -1.188547e-02 3667 39.895481 4.249029
93 93 1 -1.366958e-02 9.873455e-04 2827 22.636129 5.269634
94 94 1 1.004551e-02 5.205147e-04 3667 44.028976 3.993555
95 95 1 5.892557e-03 -5.482296e-04 2416 5.385977 4.614692
96 96 1 -1.662132e-02 -9.946494e-04 3806 42.599808 3.951163
97 97 1 -7.977792e-03 5.937776e-03 3470 28.888371 3.120762
98 98 1 -2.408042e-02 -2.647421e-03 2975 16.228737 4.227977
99 99 1 -1.191509e-02 -2.014583e-03 2461 30.051607 4.361413
100 100 1 1.110316e-02 2.506189e-04 3362 29.517509 4.591039
101 101 1 2.010373e-03 4.185408e-04 5104 17.387333 3.642855
102 102 1 -3.218945e-03 1.004196e-02 4113 44.448421 3.282414
103 103 1 2.438254e-03 2.551999e-03 3234 31.205780 3.844411
104 104 1 -1.178511e-02 2.775465e-04 1864 1.350224 3.875072
105 105 1 -9.511201e-04 -1.446065e-03 2351 22.406872 4.392300
106 106 1 -4.563018e-03 -5.890041e-03 3141 24.862123 3.998985
107 107 1 -1.471223e-02 5.965497e-03 3765 25.363234 3.661456
108 108 1 -5.857890e-03 -9.363544e-03 2272 22.878105 5.105480
109 109 1 1.369277e-02 1.019289e-02 4016 44.848000 4.092690
110 110 1 -8.784844e-03 3.358194e-03 3293 32.543564 4.115062
111 111 1 -5.148044e-03 5.372697e-03 3038 31.772562 3.626687
112 112 1 -1.556184e+35 5.799786e+34 4961 29.421440 3.020591
113 113 1 3.831991e-03 1.570888e-03 2069 28.821898 3.790284
114 114 1 8.289138e-04 6.439757e-04 2154 21.045721 3.959267
115 115 1 -4.800863e-03 3.194520e-03 5294 45.660866 3.701611
116 116 1 2.974254e-02 1.197812e-02 4380 31.670097 3.877057
117 117 1 1.137725e-02 -1.082659e-02 5172 18.774675 3.572600
118 118 1 -4.678526e-03 7.448288e-03 2257 39.260731 4.227000
119 119 1 -4.655881e-03 -1.119303e-03 3233 30.205467 5.613868
120 120 1 -4.827522e-03 -4.766134e-03 3414 42.974857 3.831894
121 121 1 -8.568994e-04 1.053632e-03 1750 29.421440 4.132886
122 122 1 1.212121e-02 0.000000e+00 5018 20.136303 3.669850
123 123 1 -4.711660e-03 -2.261143e-03 3013 45.007954 3.622240
124 124 1 -1.226328e-02 4.688181e-04 3842 26.880308 3.098333
125 125 1 3.438910e-03 1.441129e-03 3470 11.386165 4.552782
126 126 1 1.192164e-02 -1.295839e-03 3473 22.684824 4.748498
127 127 1 -1.960781e-40 0.000000e+00 4155 90.000000 2.960569
128 128 1 2.124726e-04 1.945100e-03 2496 32.103516 5.242211
129 129 1 5.669804e-03 -4.589476e-03 2577 35.398876 4.271112
130 130 1 -8.838220e-03 -9.496282e-04 4921 14.506372 4.088247
131 131 1 1.009090e-02 -2.243944e-03 3385 38.372120 4.067030
132 132 1 5.630660e-03 -8.632211e-04 4003 33.322365 3.776054
133 133 1 -9.103803e-03 -6.322661e-03 2758 47.934212 3.739807
134 134 1 6.225513e-03 -1.824928e-03 3925 37.085732 3.389725
135 135 1 -1.303080e-03 3.580316e-03 2978 27.432941 4.345174
136 136 1 1.355920e-02 3.468190e-03 5058 57.797195 3.739124
137 137 1 2.092464e-02 -3.244962e-04 2400 3.931096 3.032193
138 138 1 5.691811e-02 -7.933985e-04 3885 15.069956 3.414036
139 139 1 8.052407e-05 -3.197287e-03 3493 33.993008 3.881695
140 140 1 -1.892967e-02 -5.049255e-03 2985 24.904482 4.417928
141 141 1 2.278842e-02 1.188287e-02 3666 31.670097 3.313449
142 142 1 1.496110e-02 2.181270e-03 3702 30.498932 3.171413
[ reached 'max' / getOption("max.print") -- omitted 18 rows ]
$id
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
[34] 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
[67] 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
[100] 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
[133] 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
I tried to lower the number of knots in the gam-call but didn't suceed as well...
Anyone might have an idea?
I fit the gam using the following line:
mgcv::gam(slide ~ s(curv_plan) + s(curv_prof) + s(dem) + s(slope) + s(ca), data = dataframes_new[[7]], family = binomial)
I have experienced the same issue. The root cause was that some of my categorical variables had fewer levels than k in my formula specification. To give an example:
Suppose one of the terms in my formula specification was:
s(I(pmin(example_variable, 120)), k = 5)
and the data in my example_variable had 3 levels (say, "yes", "no", "maybe"). This would throw the above-mentioned error.
In my case, I solved it by creating additional levels in my data (I was creating test data for a unit test). In other cases it could be solved by ensuring k does not exceed the number of levels in your categorical variables.
If you're using categorical variables, check if the root cause might be the same for you.
I found the solution to my problem by reading these:
https://stat.ethz.ch/pipermail/r-sig-ecology/2011-May/002148.html
https://stat.ethz.ch/pipermail/r-help/2007-October/143569.html
The error means that you tried to create a thin plate spline basis expansion with more basis functions than the variable from which the expansion is to be made has unique values.
As you don't show the model fitting code, we can't say more than that one of the smooths in the model you tried to fit didn't have enough unique values for the value of k you specific or used (if you didn't set k a default value was used).

Iterative loop to display outliers using Boxplot (car package) in R

I want to display all outliers by First column and respective column names.
I am using Boxplot from "car" package, if there is any other efficient solution with boxplot (lower case) then also let me know.
AFD2[Boxplot(AFD2$GOL), c("Catkey", "GOL")]
Catkey GOL
58 A2SC043 152
216 KU-1265 153
510 TU-49 199
I wish to write a loop which will display all outliers like above.
Catkey GOL
58 A2SC043 152
216 KU-1265 153
510 TU-49 199
Catkey NOL
25 GF-5466 50
517 yU-1869 452
378 KU-11 765
likewise.....
I have total 48 columns first column is "Catkey" and rest of he columns are readings.. GOL, ABC, EFG, PIL, GHF, etc.
Please help.
Here is how my dataframe looks like
> head(AFD2)
Record Catkey Sex GOL NOL BNL BBH XCB XFB WFB ZYB AUB ASB BPL NPH
2 2 019-CRA M 161 160 95 135 143 116 90 135 128 109 89 72
3 3 021-CRA M 174 169 109 142 139 112 87 141 131 101 95 66
4 4 023-CRA M 171 168 100 140 136 112 89 135 126 110 99 72
5 5 024-CRA F 166 167 94 130 133 100 85 124 121 104 94 63
6 6 025-CRA M 166 168 100 140 148 120 92 139 130 109 93 73
7 7 026-CRA M 165 165 98 135 146 118 89 136 129 108 93 68
NLH JUB NLB MAB MAL MDH OBH OBB DKB NDS WNB SIS ZMB SSS FMB NAS
2 52 117 29 62 48 28 36 40 20 10 10.1 4.7 99 23 95 15
3 54 121 29 61 46 30 38 43 16 7 6.2 3.5 96 19 97 13
4 54 118 26 68 52 28 34 40 18 9 6.9 4.1 100 24 95 12
5 46 108 23 60 51 25 31 37 23 8 9.0 2.5 92 23 91 14
6 53 119 26 69 54 26 36 40 22 12 9.7 4.0 97 20 98 14
7 52 120 30 68 51 30 35 38 22 7 8.8 2.3 98 23 92 14
EKB DKS IML XML MLS WMH GLS STB FRC FRS FRF PAC PAS PAF OCC OCS
2 98 10 37 55 12 25 2 116 108 24 54 98 24 56 92 28
3 98 13 40 59 14 20 4 101 106 25 53 112 27 55 88 21
4 94 8 29 51 13 25 4 113 111 26 56 114 25 62 94 23
5 93 9 33 51 11 20 2 93 107 25 49 106 23 60 97 21
6 100 6 39 56 14 25 5 112 117 20 58 101 25 48 95 20
7 96 9 32 49 9 23 2 111 113 26 55 97 23 48 94 26
OCF
2 58
3 42
4 58
5 39
6 46
7 64
> str(AFD2)
'data.frame': 526 obs. of 48 variables:
$ Record: int 2 3 4 5 6 7 8 9 10 11 ...
$ Catkey: Factor w/ 589 levels "016-CRA","019-CRA",..: 2 3 4 5 6 7 8 9 10 11 ...
$ Sex : Factor w/ 6 levels "","F","M","MALE?",..: 3 3 3 2 3 3 3 5 2 3 ...
$ GOL : int 161 174 171 166 166 165 171 157 166 183 ...
$ NOL : int 160 169 168 167 168 165 169 158 164 179 ...
$ BNL : int 95 109 100 94 100 98 99 85 94 99 ...
$ BBH : int 135 142 140 130 140 135 138 123 125 139 ...
$ XCB : int 143 139 136 133 148 146 134 127 132 141 ...
$ XFB : int 116 112 112 100 120 118 109 105 107 118 ...
$ WFB : int 90 87 89 85 92 89 93 81 85 95 ...
$ ZYB : int 135 141 135 124 139 136 131 104 120 137 ...
$ AUB : int 128 131 126 121 130 129 120 103 116 127 ...
$ ASB : int 109 101 110 104 109 108 105 96 101 107 ...
$ BPL : int 89 95 99 94 93 93 93 75 94 98 ...
$ NPH : int 72 66 72 63 73 68 62 54 64 68 ...
$ NLH : int 52 54 54 46 53 52 51 42 48 49 ...
$ JUB : int 117 121 118 108 119 120 116 91 104 123 ...
$ NLB : int 29 29 26 23 26 30 28 21 24 28 ...
$ MAB : int 62 61 68 60 69 68 66 48 60 69 ...
$ MAL : int 48 46 52 51 54 51 49 37 48 53 ...
$ MDH : int 28 30 28 25 26 30 32 15 25 31 ...
$ OBH : int 36 38 34 31 36 35 35 32 33 32 ...
$ OBB : int 40 43 40 37 40 38 38 34 36 37 ...
$ DKB : int 20 16 18 23 22 22 25 15 19 23 ...
$ NDS : int 10 7 9 8 12 7 9 6 7 10 ...
$ WNB : num 10.1 6.2 6.9 9 9.7 8.8 9.6 5.8 6.9 6.8 ...
$ SIS : num 4.7 3.5 4.1 2.5 4 2.3 3 1.7 1.7 1.9 ...
$ ZMB : int 99 96 100 92 97 98 97 71 92 98 ...
$ SSS : int 23 19 24 23 20 23 23 19 21 23 ...
$ FMB : int 95 97 95 91 98 92 95 79 90 99 ...
$ NAS : int 15 13 12 14 14 14 17 13 11 14 ...
$ EKB : int 98 98 94 93 100 96 98 80 91 98 ...
$ DKS : int 10 13 8 9 6 9 10 11 6 7 ...
$ IML : int 37 40 29 33 39 32 37 30 31 36 ...
$ XML : int 55 59 51 51 56 49 55 48 51 56 ...
$ MLS : int 12 14 13 11 14 9 13 9 14 14 ...
$ WMH : int 25 20 25 20 25 23 22 19 22 27 ...
$ GLS : int 2 4 4 2 5 2 2 1 2 4 ...
$ STB : int 116 101 113 93 112 111 108 105 107 111 ...
$ FRC : int 108 106 111 107 117 113 109 99 100 116 ...
$ FRS : int 24 25 26 25 20 26 28 22 25 28 ...
$ FRF : int 54 53 56 49 58 55 47 46 51 49 ...
$ PAC : int 98 112 114 106 101 97 115 101 105 104 ...
$ PAS : int 24 27 25 23 25 23 26 24 23 21 ...
$ PAF : int 56 55 62 60 48 48 57 52 52 47 ...
$ OCC : int 92 88 94 97 95 94 90 92 96 112 ...
$ OCS : int 28 21 23 21 20 26 27 20 28 40 ...
$ OCF : int 58 42 58 39 46 64 50 49 51 71 ...
- attr(*, "na.action")=Class 'omit' Named int [1:63] 1 12 18 20 23 24 29 33 35 39 ...
.. ..- attr(*, "names")= chr [1:63] "1" "12" "18" "20" ...

Group rows based on time window and sum observations

Here an example of my data.frame:
df_1 = read.table(text='ID Day Episode Count
1 30001 7423 47 16
2 33021 7423 47 16
3 33024 7423 47 16
4 33034 7423 47 16
5 37018 7423 47 16
6 40004 7423 47 16
7 40011 7423 47 16
8 41028 7423 47 16
9 42001 7423 47 16
10 42011 7423 47 16
11 45003 7423 47 16
12 45004 7423 47 16
13 45005 7423 47 16
14 46006 7423 47 16
15 46008 7423 47 16
16 47004 7423 47 16
17 2001 7438 54 13
18 19007 7438 54 13
19 20002 7438 54 13
20 22006 7438 54 13
21 22007 7438 54 13
22 29002 7438 54 13
23 29003 7438 54 13
24 30001 7438 54 13
25 30004 7438 54 13
26 33023 7438 54 13
27 33029 7438 54 13
28 41006 7438 54 13
29 41020 7438 54 13
30 21009 7428 65 12
31 24001 7428 65 12
32 25005 7428 65 12
33 25009 7428 65 12
34 27002 7428 65 12
35 27003 7428 65 12
36 27009 7428 65 12
37 30001 7428 65 12
38 33023 7428 65 12
39 33029 7428 65 12
40 33050 7428 65 12
41 34003 7428 65 12
42 14001 7427 81 10
43 16004 7427 81 10
44 17001 7427 81 10
45 17005 7427 81 10
46 19001 7427 81 10
47 19006 7427 81 10
48 19007 7427 81 10
49 19010 7427 81 10
50 20001 7427 81 10
51 21009 7427 81 10
52 28047 7424 143 9
53 28049 7424 143 9
54 29002 7424 143 9
55 29003 7424 143 9
56 30003 7424 143 9
57 30004 7424 143 9
58 32010 7424 143 9
59 33014 7424 143 9
60 33023 7424 143 9
104 30001 6500 111 9
105 33021 6500 111 9
106 33024 6500 111 9
107 33034 6500 111 9
108 37018 6500 111 9
109 40004 6500 111 9
110 40011 6500 111 9
111 41028 6500 111 9
112 42001 6500 111 9
61 25005 7422 158 8
62 27021 7422 158 8
63 28015 7422 158 8
64 28047 7422 158 8
65 28049 7422 158 8
66 29001 7422 158 8
67 29002 7422 158 8
68 29003 7422 158 8
69 27002 7425 246 6
70 27003 7425 246 6
71 27021 7425 246 6
72 33006 7425 246 6
73 34001 7425 246 6
74 37019 7425 246 6
75 33014 7429 979 5
76 33021 7429 979 5
77 33024 7429 979 5
78 34001 7429 979 5
79 35010 7429 979 5
80 28022 7426 1199 5
81 34006 7426 1199 5
82 37006 7426 1199 5
83 37008 7426 1199 5
84 37018 7426 1199 5
85 29001 7437 1756 4
86 30014 7437 1756 4
87 32010 7437 1756 4
88 45004 7437 1756 4
89 4003 7430 1757 4
90 15013 7430 1757 4
91 16004 7430 1757 4
92 43007 7430 1757 4
93 7002 7434 1570 4
94 8006 7434 1570 4
95 15006 7434 1570 4
96 94001 7434 1570 4
113 33024 6499 135 4
114 33034 6499 135 4
115 37018 6499 135 4
116 40004 6499 135 4
222 3005 7440 999 2
223 3400 7440 999 2
97 3002 7433 2295 2
98 4003 7433 2295 2
99 48005 7436 3389 2
100 49004 7436 3389 2
101 8006 7431 3390 2
102 15006 7431 3390 2
104 6780 7439 22 1
103 41020 7435 4511 1', header = TRUE)
The data.frame has got this order based on -Count and -Day and it cannot be changed.
What I need to do is to group the data.frame by Day and its previous 16 days (17 days in total) and sum the Count column's observations. So in this case the largest Day observation with largest Count is 7438, then take 7438, 7437, 7436, 7435, etc.. up to 7424 and sum the Count values. The next Day observation which cannot be included in the 17-days time window of 7438 is 6500. Then look for 6500, 6499, etc... and do the same as per 7438 group. And do the same for Day 7440 and 7439.
The thing is that if we start to count the Day backwards from the top of the Day col, Day = 7423 will include in its Episode Day = 7422 and both of them will be trowed away from being included in the Day = 7438 Episode.
How can we write a code that first kind of analyse all the possible Episodes' combinations (based on the time window) and eventually select the one which cover the largest number of days (no more than the time window)?
Expected output:
ID Day Episode Count
1 2001 7438 1 103
2 19007 7438 1 103
3 20002 7438 1 103
4 22006 7438 1 103
5 22007 7438 1 103
6 29002 7438 1 103
7 29003 7438 1 103
8 30001 7438 1 103
9 30004 7438 1 103
10 33023 7438 1 103
11 33029 7438 1 103
12 41006 7438 1 103
13 41020 7438 1 103
14 29001 7437 1 103
15 30014 7437 1 103
16 32010 7437 1 103
17 45004 7437 1 103
18 48005 7436 1 103
19 49004 7436 1 103
20 41020 7435 1 103
21 7002 7434 1 103
22 8006 7434 1 103
23 15006 7434 1 103
24 94001 7434 1 103
25 3002 7433 1 103
26 4003 7433 1 103
27 8006 7431 1 103
28 15006 7431 1 103
29 4003 7430 1 103
30 15013 7430 1 103
31 16004 7430 1 103
32 43007 7430 1 103
33 33014 7429 1 103
34 33021 7429 1 103
35 33024 7429 1 103
36 34001 7429 1 103
37 35010 7429 1 103
38 21009 7428 1 103
39 24001 7428 1 103
40 25005 7428 1 103
41 25009 7428 1 103
42 27002 7428 1 103
43 27003 7428 1 103
44 27009 7428 1 103
45 30001 7428 1 103
46 33023 7428 1 103
47 33029 7428 1 103
48 33050 7428 1 103
49 34003 7428 1 103
50 14001 7427 1 103
51 16004 7427 1 103
52 17001 7427 1 103
53 17005 7427 1 103
54 19001 7427 1 103
55 19006 7427 1 103
56 19007 7427 1 103
57 19010 7427 1 103
58 20001 7427 1 103
59 21009 7427 1 103
60 28022 7426 1 103
61 34006 7426 1 103
62 37006 7426 1 103
63 37008 7426 1 103
64 37018 7426 1 103
65 27002 7425 1 103
66 27003 7425 1 103
67 27021 7425 1 103
68 33006 7425 1 103
69 34001 7425 1 103
70 37019 7425 1 103
71 28047 7424 1 103
72 28049 7424 1 103
73 29002 7424 1 103
74 29003 7424 1 103
75 30003 7424 1 103
76 30004 7424 1 103
77 32010 7424 1 103
78 33014 7424 1 103
79 33023 7424 1 103
80 30001 7423 1 103
81 33021 7423 1 103
82 33024 7423 1 103
83 33034 7423 1 103
84 37018 7423 1 103
85 40004 7423 1 103
86 40011 7423 1 103
87 41028 7423 1 103
88 42001 7423 1 103
89 42011 7423 1 103
90 45003 7423 1 103
91 45004 7423 1 103
92 45005 7423 1 103
93 46006 7423 1 103
94 46008 7423 1 103
95 47004 7423 1 103
96 25005 7422 1 103
97 27021 7422 1 103
98 28015 7422 1 103
99 28047 7422 1 103
100 28049 7422 1 103
101 29001 7422 1 103
102 29002 7422 1 103
103 29003 7422 1 103
104 30001 6500 2 13
105 33021 6500 2 13
106 33024 6500 2 13
107 33034 6500 2 13
108 37018 6500 2 13
109 40004 6500 2 13
110 40011 6500 2 13
111 41028 6500 2 13
112 42001 6500 2 13
113 33024 6499 2 13
114 33034 6499 2 13
115 37018 6499 2 13
116 40004 6499 2 13
117 3005 7440 3 3
118 3400 7440 3 3
119 6780 7439 3 3
My real data.frame has got >40,000 rows and >1600 episodes.
There is probably a better way to accomplish this, but something like this may get you what you need.
library(dplyr)
groups <- seq(max(df_1$Day)+1,min(df_1$Day),by = -17)
groups <- rev(append(groups, min(df_1$Day)))
df_1$group <- groups[cut(df_1$Day, breaks = groups, labels = FALSE, right = FALSE)]
df_1 <- df_1 %>%
group_by(group) %>%
mutate(TotalPerGroup = sum(Count))

Why Decile values are incorrect using the cut function

I tried to attach a decile value for each observation using the code below.However, it seems that the values are not correct. What can be the reason for that?
df<-read.table(text="pregnant glucose blood skin INSULIN MASS DIAB AGE CLASS predict_probability
1 106 70 28 135 34.2 0.142 22 0 0.15316285
1 91 54 25 100 25.2 0.234 23 0 0.05613959
4 136 70 0 0 31.2 1.182 22 1 0.54034794
9 164 78 0 0 32.8 0.148 45 1 0.64361578
3 173 78 39 185 33.8 0.970 31 1 0.79185196
11 136 84 35 130 28.3 0.260 42 1 0.31927737
0 141 84 26 0 32.4 0.433 22 0 0.41609308
3 106 72 0 0 25.8 0.207 27 0 0.10460090
9 145 80 46 130 37.9 0.637 40 1 0.67061324
10 111 70 27 0 27.5 0.141 40 1 0.16152296
",header=T)
deciles <- cut(df$predict_probability, breaks=c(quantile(df$predict_probability, probs = seq(0, 1, by = 0.10))),labels = 1:10, include.lowest=TRUE)
df1 <- cbind(df,deciles)
head(df1,10)
pregnant glucose blood skin INSULIN MASS DIAB AGE CLASS predict_probability deciles
1 1 106 70 28 135 34.2 0.142 22 0 0.15316285 3
2 1 91 54 25 100 25.2 0.234 23 0 0.05613959 1
3 4 136 70 0 0 31.2 1.182 22 1 0.54034794 7
4 9 164 78 0 0 32.8 0.148 45 1 0.64361578 8
5 3 173 78 39 185 33.8 0.970 31 1 0.79185196 10
6 11 136 84 35 130 28.3 0.260 42 1 0.31927737 5
7 0 141 84 26 0 32.4 0.433 22 0 0.41609308 6
8 3 106 72 0 0 25.8 0.207 27 0 0.10460090 2
9 9 145 80 46 130 37.9 0.637 40 1 0.67061324 9
10 10 111 70 27 0 27.5 0.141 40 1 0.16152296 4
Per Dason's proposal, here is the full answer to the question.
The quantile function should be taken out from the code so seq(0,1,by=0.1) should be passed directly to the cut function.
deciles <- cut(df$predict_probability, seq(0,1,by=0.1) ,labels = 1:10, include.lowest=TRUE)
df1 <- cbind(df,deciles)
head(df1,10)
pregnant glucose blood skin INSULIN MASS DIAB AGE CLASS predict_probability deciles
1 1 106 70 28 135 34.2 0.142 22 0 0.15316285 2
2 1 91 54 25 100 25.2 0.234 23 0 0.05613959 1
3 4 136 70 0 0 31.2 1.182 22 1 0.54034794 6
4 9 164 78 0 0 32.8 0.148 45 1 0.64361578 7
5 3 173 78 39 185 33.8 0.970 31 1 0.79185196 8
6 11 136 84 35 130 28.3 0.260 42 1 0.31927737 4
7 0 141 84 26 0 32.4 0.433 22 0 0.41609308 5
8 3 106 72 0 0 25.8 0.207 27 0 0.10460090 2
9 9 145 80 46 130 37.9 0.637 40 1 0.67061324 7
10 10 111 70 27 0 27.5 0.141 40 1 0.16152296 2

Sequence with different intervals in R: matching sensor data

I need a vector that repeats numbers in a sequence at varying intervals. I basically need this
c(rep(1:42, each=6), rep(43:64, each = 7),
rep(65:106, each=6), rep(107:128, each = 7),
.... but I need to this to keep going, until almost 2 million.
So I want a vector that looks like
[1] 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 5 ...
.....
[252] 43 43 43 43 43 43 43 44 44 44 44 44 44 44
....
[400] 64 64 64 64 64 64 65 65 65 65 65 65...
and so on. Not just alternating between 6 and 7 repetitions, rather mostly 6s and fewer 7s until the whole vector is something like 1.7 million rows. So, is there a loop I can do? Or apply, replicate? I need the 400th entry in the vector to be 64, the 800th entry to be 128, and so on, in somewhat evenly spaced integers.
UPDATE
Thank you all for the quick clever tricks there. It worked, at least well enough for the deadline I was dealing with. I realize repeating 6 xs and 7 xs are a really dumb way to try to solve this, but it was quick at least. But now that I have some time, I would like to get everyone's opinions /ideas on my real underlying issue here.
I have two datasets to merge. They are both sensor datasets, both with stopwatch time as primary keys. But one records every 1/400 of a second, and the other records every 1/256 of a second. I have trimmed the top of each so that they are starting the exact same moment. But.. now what? I have 400 records for each second in one set, and 256 records for 1 second in the other. Is there a way to merge these without losing data? Interpolating or just repeating obs is a-ok, necessary, I think, but I'd rather not throw any data out.
I read this post here, that had to do with using xts and zoo for a very similar problem to mine. But they have nice epoch date/times for each. I just have these awful fractions of seconds!
sample data (A):
time dist a_lat
1 139.4300 22 0
2 139.4325 22 0
3 139.4350 22 0
4 139.4375 22 0
5 139.4400 22 0
6 139.4425 22 0
7 139.4450 22 0
8 139.4475 22 0
9 139.4500 22 0
10 139.4525 22 0
sample data (B):
timestamp hex_acc_x hex_acc_y hex_acc_z
1 367065215501 -0.5546875 -0.7539062 0.1406250
2 367065215505 -0.5468750 -0.7070312 0.2109375
3 367065215509 -0.4218750 -0.6835938 0.1796875
4 367065215513 -0.5937500 -0.7421875 0.1562500
5 367065215517 -0.6757812 -0.7773438 0.2031250
6 367065215521 -0.5937500 -0.8554688 0.2460938
7 367065215525 -0.6132812 -0.8476562 0.2109375
8 367065215529 -0.3945312 -0.8906250 0.2031250
9 367065215533 -0.3203125 -0.8906250 0.2226562
10 367065215537 -0.3867188 -0.9531250 0.2578125
(oh yeah, and btw, the B dataset timestamps are epoch format * 256, because life is hard. i haven't converted it for this because dataset A has nothing like that, only just 0.0025 intervals. Also the B data sensor was left on for hours later the A data sensor turned off, so that doesn't help)
Or if you like, you can try this using apply
# using this sample data
df <- data.frame(from=c(1,4,7,11), to = c(3,6,10,13),rep=c(6,7,6,7));
> df
# from to rep
#1 1 3 6
#2 4 6 7
#3 7 10 6
#4 11 13 7
unlist(apply(df, 1, function(x) rep(x['from']:x['to'], each=x['rep'])))
# [1] 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 4
#[26] 5 5 5 5 5 5 5 6 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8
#[51] 8 9 9 9 9 9 9 10 10 10 10 10 10 11 11 11 11 11 11 11 12 12 12 12 12
#[76] 12 12 13 13 13 13 13 13 13
Now that you put it that way ... I have absolutely no idea how you are planning on using all of the 6s and 7s. :-)
Regardless, I recommend standardizing the time, adding a "sample" column, and merging on them. Having the "sample" column may facilitate your processing later on, perhaps.
Your data:
df400 <- structure(list(time = c(139.43, 139.4325, 139.435, 139.4375, 139.44, 139.4425,
139.445, 139.4475, 139.45, 139.4525),
dist = c(22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L),
a_lat = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)),
.Names = c("time", "dist", "a_lat"),
class = "data.frame", row.names = c(NA, -10L))
df256 <- structure(list(timestamp = c(367065215501, 367065215505, 367065215509, 367065215513,
367065215517, 367065215521, 367065215525, 367065215529,
367065215533, 367065215537),
hex_acc_x = c(-0.5546875, -0.546875, -0.421875, -0.59375, -0.6757812,
-0.59375, -0.6132812, -0.3945312, -0.3203125, -0.3867188),
hex_acc_y = c(-0.7539062, -0.7070312, -0.6835938, -0.7421875,
-0.7773438, -0.8554688, -0.8476562, -0.890625,
-0.890625, -0.953125),
hex_acc_z = c(0.140625, 0.2109375, 0.1796875, 0.15625, 0.203125,
0.2460938, 0.2109375, 0.203125, 0.2226562, 0.2578125)),
.Names = c("timestamp", "hex_acc_x", "hex_acc_y", "hex_acc_z"),
class = "data.frame", row.names = c(NA, -10L))
Standardize your time frames:
colnames(df256)[1] <- 'time'
df400$time <- df400$time - df400$time[1]
df256$time <- (df256$time - df256$time[1]) / 256
Assign a label for easy reference (not that the NAs won't be clear enough):
df400 <- cbind(sample='A', df400, stringsAsFactors=FALSE)
df256 <- cbind(sample='B', df256, stringsAsFactors=FALSE)
And now for the merge and sorting:
dat <- merge(df400, df256, by=c('sample', 'time'), all.x=TRUE, all.y=TRUE)
dat <- dat[order(dat$time),]
dat
## sample time dist a_lat hex_acc_x hex_acc_y hex_acc_z
## 1 A 0.000000 22 0 NA NA NA
## 11 B 0.000000 NA NA -0.5546875 -0.7539062 0.1406250
## 2 A 0.002500 22 0 NA NA NA
## 3 A 0.005000 22 0 NA NA NA
## 4 A 0.007500 22 0 NA NA NA
## 5 A 0.010000 22 0 NA NA NA
## 6 A 0.012500 22 0 NA NA NA
## 7 A 0.015000 22 0 NA NA NA
## 12 B 0.015625 NA NA -0.5468750 -0.7070312 0.2109375
## 8 A 0.017500 22 0 NA NA NA
## 9 A 0.020000 22 0 NA NA NA
## 10 A 0.022500 22 0 NA NA NA
## 13 B 0.031250 NA NA -0.4218750 -0.6835938 0.1796875
## 14 B 0.046875 NA NA -0.5937500 -0.7421875 0.1562500
## 15 B 0.062500 NA NA -0.6757812 -0.7773438 0.2031250
## 16 B 0.078125 NA NA -0.5937500 -0.8554688 0.2460938
## 17 B 0.093750 NA NA -0.6132812 -0.8476562 0.2109375
## 18 B 0.109375 NA NA -0.3945312 -0.8906250 0.2031250
## 19 B 0.125000 NA NA -0.3203125 -0.8906250 0.2226562
## 20 B 0.140625 NA NA -0.3867188 -0.9531250 0.2578125
I'm guessing your data was just a small representation. If I've guessed poorly (that A's integers are seconds and B's integers are 1/400ths of a second) then just scale differently. Either way, by resetting the first value to zero and then merging/sorting, they are easy to merge and sort.
alt <- data.frame(len=c(42,22),rep=c(6,7));
alt;
## len rep
## 1 42 6
## 2 22 7
altrep <- function(alt,cyc,len) {
cyclen <- sum(alt$len*alt$rep);
if (missing(cyc)) {
if (missing(len)) {
cyc <- 1;
len <- cyc*cyclen;
} else {
cyc <- ceiling(len/cyclen);
};
} else if (missing(len)) {
len <- cyc*cyclen;
};
if (isTRUE(all.equal(len,0))) return(integer());
result <- rep(1:(cyc*sum(alt$len)),rep(rep(alt$rep,alt$len),cyc));
length(result) <- len;
result;
};
altrep(alt,2);
## [1] 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8 8 9 9 9
## [52] 9 9 9 10 10 10 10 10 10 11 11 11 11 11 11 12 12 12 12 12 12 13 13 13 13 13 13 14 14 14 14 14 14 15 15 15 15 15 15 16 16 16 16 16 16 17 17 17 17 17 17
## [103] 18 18 18 18 18 18 19 19 19 19 19 19 20 20 20 20 20 20 21 21 21 21 21 21 22 22 22 22 22 22 23 23 23 23 23 23 24 24 24 24 24 24 25 25 25 25 25 25 26 26 26
## [154] 26 26 26 27 27 27 27 27 27 28 28 28 28 28 28 29 29 29 29 29 29 30 30 30 30 30 30 31 31 31 31 31 31 32 32 32 32 32 32 33 33 33 33 33 33 34 34 34 34 34 34
## [205] 35 35 35 35 35 35 36 36 36 36 36 36 37 37 37 37 37 37 38 38 38 38 38 38 39 39 39 39 39 39 40 40 40 40 40 40 41 41 41 41 41 41 42 42 42 42 42 42 43 43 43
## [256] 43 43 43 43 44 44 44 44 44 44 44 45 45 45 45 45 45 45 46 46 46 46 46 46 46 47 47 47 47 47 47 47 48 48 48 48 48 48 48 49 49 49 49 49 49 49 50 50 50 50 50
## [307] 50 50 51 51 51 51 51 51 51 52 52 52 52 52 52 52 53 53 53 53 53 53 53 54 54 54 54 54 54 54 55 55 55 55 55 55 55 56 56 56 56 56 56 56 57 57 57 57 57 57 57
## [358] 58 58 58 58 58 58 58 59 59 59 59 59 59 59 60 60 60 60 60 60 60 61 61 61 61 61 61 61 62 62 62 62 62 62 62 63 63 63 63 63 63 63 64 64 64 64 64 64 64 65 65
## [409] 65 65 65 65 66 66 66 66 66 66 67 67 67 67 67 67 68 68 68 68 68 68 69 69 69 69 69 69 70 70 70 70 70 70 71 71 71 71 71 71 72 72 72 72 72 72 73 73 73 73 73
## [460] 73 74 74 74 74 74 74 75 75 75 75 75 75 76 76 76 76 76 76 77 77 77 77 77 77 78 78 78 78 78 78 79 79 79 79 79 79 80 80 80 80 80 80 81 81 81 81 81 81 82 82
## [511] 82 82 82 82 83 83 83 83 83 83 84 84 84 84 84 84 85 85 85 85 85 85 86 86 86 86 86 86 87 87 87 87 87 87 88 88 88 88 88 88 89 89 89 89 89 89 90 90 90 90 90
## [562] 90 91 91 91 91 91 91 92 92 92 92 92 92 93 93 93 93 93 93 94 94 94 94 94 94 95 95 95 95 95 95 96 96 96 96 96 96 97 97 97 97 97 97 98 98 98 98 98 98 99 99
## [613] 99 99 99 99 100 100 100 100 100 100 101 101 101 101 101 101 102 102 102 102 102 102 103 103 103 103 103 103 104 104 104 104 104 104 105 105 105 105 105 105 106 106 106 106 106 106 107 107 107 107 107
## [664] 107 107 108 108 108 108 108 108 108 109 109 109 109 109 109 109 110 110 110 110 110 110 110 111 111 111 111 111 111 111 112 112 112 112 112 112 112 113 113 113 113 113 113 113 114 114 114 114 114 114 114
## [715] 115 115 115 115 115 115 115 116 116 116 116 116 116 116 117 117 117 117 117 117 117 118 118 118 118 118 118 118 119 119 119 119 119 119 119 120 120 120 120 120 120 120 121 121 121 121 121 121 121 122 122
## [766] 122 122 122 122 122 123 123 123 123 123 123 123 124 124 124 124 124 124 124 125 125 125 125 125 125 125 126 126 126 126 126 126 126 127 127 127 127 127 127 127 128 128 128 128 128 128 128
altrep(alt,len=1000);
## [1] 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8 8 9 9 9
## [52] 9 9 9 10 10 10 10 10 10 11 11 11 11 11 11 12 12 12 12 12 12 13 13 13 13 13 13 14 14 14 14 14 14 15 15 15 15 15 15 16 16 16 16 16 16 17 17 17 17 17 17
## [103] 18 18 18 18 18 18 19 19 19 19 19 19 20 20 20 20 20 20 21 21 21 21 21 21 22 22 22 22 22 22 23 23 23 23 23 23 24 24 24 24 24 24 25 25 25 25 25 25 26 26 26
## [154] 26 26 26 27 27 27 27 27 27 28 28 28 28 28 28 29 29 29 29 29 29 30 30 30 30 30 30 31 31 31 31 31 31 32 32 32 32 32 32 33 33 33 33 33 33 34 34 34 34 34 34
## [205] 35 35 35 35 35 35 36 36 36 36 36 36 37 37 37 37 37 37 38 38 38 38 38 38 39 39 39 39 39 39 40 40 40 40 40 40 41 41 41 41 41 41 42 42 42 42 42 42 43 43 43
## [256] 43 43 43 43 44 44 44 44 44 44 44 45 45 45 45 45 45 45 46 46 46 46 46 46 46 47 47 47 47 47 47 47 48 48 48 48 48 48 48 49 49 49 49 49 49 49 50 50 50 50 50
## [307] 50 50 51 51 51 51 51 51 51 52 52 52 52 52 52 52 53 53 53 53 53 53 53 54 54 54 54 54 54 54 55 55 55 55 55 55 55 56 56 56 56 56 56 56 57 57 57 57 57 57 57
## [358] 58 58 58 58 58 58 58 59 59 59 59 59 59 59 60 60 60 60 60 60 60 61 61 61 61 61 61 61 62 62 62 62 62 62 62 63 63 63 63 63 63 63 64 64 64 64 64 64 64 65 65
## [409] 65 65 65 65 66 66 66 66 66 66 67 67 67 67 67 67 68 68 68 68 68 68 69 69 69 69 69 69 70 70 70 70 70 70 71 71 71 71 71 71 72 72 72 72 72 72 73 73 73 73 73
## [460] 73 74 74 74 74 74 74 75 75 75 75 75 75 76 76 76 76 76 76 77 77 77 77 77 77 78 78 78 78 78 78 79 79 79 79 79 79 80 80 80 80 80 80 81 81 81 81 81 81 82 82
## [511] 82 82 82 82 83 83 83 83 83 83 84 84 84 84 84 84 85 85 85 85 85 85 86 86 86 86 86 86 87 87 87 87 87 87 88 88 88 88 88 88 89 89 89 89 89 89 90 90 90 90 90
## [562] 90 91 91 91 91 91 91 92 92 92 92 92 92 93 93 93 93 93 93 94 94 94 94 94 94 95 95 95 95 95 95 96 96 96 96 96 96 97 97 97 97 97 97 98 98 98 98 98 98 99 99
## [613] 99 99 99 99 100 100 100 100 100 100 101 101 101 101 101 101 102 102 102 102 102 102 103 103 103 103 103 103 104 104 104 104 104 104 105 105 105 105 105 105 106 106 106 106 106 106 107 107 107 107 107
## [664] 107 107 108 108 108 108 108 108 108 109 109 109 109 109 109 109 110 110 110 110 110 110 110 111 111 111 111 111 111 111 112 112 112 112 112 112 112 113 113 113 113 113 113 113 114 114 114 114 114 114 114
## [715] 115 115 115 115 115 115 115 116 116 116 116 116 116 116 117 117 117 117 117 117 117 118 118 118 118 118 118 118 119 119 119 119 119 119 119 120 120 120 120 120 120 120 121 121 121 121 121 121 121 122 122
## [766] 122 122 122 122 122 123 123 123 123 123 123 123 124 124 124 124 124 124 124 125 125 125 125 125 125 125 126 126 126 126 126 126 126 127 127 127 127 127 127 127 128 128 128 128 128 128 128 129 129 129 129
## [817] 129 129 130 130 130 130 130 130 131 131 131 131 131 131 132 132 132 132 132 132 133 133 133 133 133 133 134 134 134 134 134 134 135 135 135 135 135 135 136 136 136 136 136 136 137 137 137 137 137 137 138
## [868] 138 138 138 138 138 139 139 139 139 139 139 140 140 140 140 140 140 141 141 141 141 141 141 142 142 142 142 142 142 143 143 143 143 143 143 144 144 144 144 144 144 145 145 145 145 145 145 146 146 146 146
## [919] 146 146 147 147 147 147 147 147 148 148 148 148 148 148 149 149 149 149 149 149 150 150 150 150 150 150 151 151 151 151 151 151 152 152 152 152 152 152 153 153 153 153 153 153 154 154 154 154 154 154 155
## [970] 155 155 155 155 155 156 156 156 156 156 156 157 157 157 157 157 157 158 158 158 158 158 158 159 159 159 159 159 159 160 160
You can specify len=1.7e6 (and omit the cyc argument) to get exactly 1.7 million elements, or you can get a whole number of cycles using cyc.
How about
len <- 2e6
step <- 400
x <- rep(64 * seq(0, ceiling(len / step) - 1), each = step) +
sort(rep(1:64, length.out = step))
x <- x[seq(len)] # to get rid of extra elements

Resources