'Find the number of properties of a given set in R
See the figure below that I have information about 2 clusters. Properties refers to which properties are in the respective cluster. What I would like to do is:
Insert a column after Cluster column, called Quantity, which is the number of properties in that particular cluster. How to do this?
library(DT)
df1<-structure(list(Cluster = 2:1, A1 = c(12656.2899894137,
15841.1160290423), A2 = c(297210.66395,
906936.79935), Properties = c("30, 32, 33, 34, 39, 41, 47, 48, 49, 50, 51, 52, 53, 54, 56, 57, 58, 59, 60, 61, 62, 63, 115, 116, 117, 118, 119, 120, 122, 123, 124, 126, 154, 155, 156, 157, 158, 159, 300, 302, 303, 304, 306, 307, 308, 309, 310, 311, 312, 314, 317, 319, 320",
"2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 29, 36, 37, 38, 42, 43, 44, 45, 64, 65, 66, 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, 102, 103, 105, 106, 107, 108, 109, 110, 112, 113, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 267, 268, 269, 270, 271, 273, 274, 275, 276, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 294, 296, 297, 321, 322, 323, 325"
)), row.names = c(NA, -2L), class = "data.frame")
x <- datatable(df1[order(df1$Cluster),c(1,4,2,3)],
options = list(columnDefs = list(list(className = 'dt-center', targets = 0:3)),
paging =TRUE,searching = FALSE,
pageLength = 10,lenghtMenu=c(5,10,15,20),scrollx=T
), rownames = FALSE)%>% formatRound(c(3:4), 2,mark = ",")%>%
formatStyle(columns = c(3:4), 'text-align' = 'center')
Solution 1:[1]
Here is one potential solution:
library(dplyr, warn.conflicts = FALSE)
library(DT)
df1<-structure(list(Cluster = 2:1, A1 = c(12656.2899894137,
15841.1160290423), A2 = c(297210.66395,
906936.79935), Properties = c("30, 32, 33, 34, 39, 41, 47, 48, 49, 50, 51, 52, 53, 54, 56, 57, 58, 59, 60, 61, 62, 63, 115, 116, 117, 118, 119, 120, 122, 123, 124, 126, 154, 155, 156, 157, 158, 159, 300, 302, 303, 304, 306, 307, 308, 309, 310, 311, 312, 314, 317, 319, 320",
"2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 29, 36, 37, 38, 42, 43, 44, 45, 64, 65, 66, 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, 102, 103, 105, 106, 107, 108, 109, 110, 112, 113, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 267, 268, 269, 270, 271, 273, 274, 275, 276, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 294, 296, 297, 321, 322, 323, 325"
)), row.names = c(NA, -2L), class = "data.frame")
df1$Quantity <- lengths(strsplit(df1$Properties, split = ", ", fixed = TRUE))
x <- datatable(df1[order(df1$Cluster),c(1,4,5,2,3)],
options = list(columnDefs = list(list(className = 'dt-center', targets = 0:3)),
paging =TRUE,searching = FALSE,
pageLength = 10,lenghtMenu=c(5,10,15,20),scrollx=T
), rownames = FALSE)%>% formatRound(c(4:5), 2,mark = ",")%>%
formatStyle(columns = c(3:5), 'text-align' = 'center')
x

Created on 2022-03-16 by the reprex package (v2.0.1)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jared_mamrot |

