subset a data frame by group [duplicate] - r

This question already has answers here:
Split a large dataframe into a list of data frames based on common value in column
(3 answers)
Closed 5 years ago.
I have a dataframe:
dput(test)
test <- structure(list(Blocking = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L), Treatment = structure(c(1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L,
2L, 3L, 4L), .Label = c("A", "B", "C", "D"), class = "factor"),
ID69 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), ID70 = c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), ID71 = c(0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 0L,
1L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L), ID72 = c(0L,
0L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 1L, 0L), ID73 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L),
ID74 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), ID75 = c(0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L), ID77 = c(0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L,
1L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L)), .Names = c("Blocking",
"Treatment", "ID69", "ID70", "ID71", "ID72", "ID73", "ID74",
"ID75", "ID77"), class = "data.frame", row.names = c(NA, -36L
))
I would like to split it based on treatment into four data frames. I tried the following code:
treatments <- c("A", "B", "C", "D")
subset_list <- lapply(treatments, function(x, input_df = test){
subset(input_dt, treatment=treatments)
})
names(subset_list) <- treatments
I get a list but individual data frames are the full test df's and do not contain rows only corresponding to the treatment. This seems like a really easy question, but I am missing a crucial detail here and I am new to the apply family. Please let me know, thanks!

You can use split():
split(test, treatments)
res <- split(x, f) divides the data into the groups defined by f into a list.
unsplit(res, f) does the opposite.

Related

Group Data in R by n number of rows

I have a small dataset from a survey. There are 18 combinations of attributes for TVs, and 15 people rated each combination from 1-9.
I have the data in the following columns in a csv (with the values in parenthesis each in a different row):
Combinations (1,2, 3,...,18), Panel_LCD (0,1,1,...,0), Panel_OLED(1,0,0,...,0), Panel_QLED(0,0,0,...,1), Res_HD(1,0,1,...,0), Res_4K(0,1,1,...,1), and same for 3 different sizes and 3 different price points. Lastly, one more column for the 1-9 rating each person gave each combination.
Part of my assignment is to show the relative importance of each attribute for each person's set of answers.
I'm going crazy trying to figure out how to do some kind of a "for" statement for this.
My alternative is to copy/paste the code they gave me 15 times (too much to even copy here!)
dput(tv_data)
output:
structure(list(Perfil = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 13L, 14L, 15L, 16L, 17L, 18L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L,
17L, 18L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 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, 3L, 4L, 4L, 4L, 6L, 5L, 4L)), class = "data.frame", row.names = c(NA,
-270L))
Here is a simple way to look at the mean rating for each combination:
ratings <- tapply(tv_data$preferencias, tv_data$Perfil, mean)
print(ratings, digits=4)
# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# 5.533 3.933 4.800 7.667 6.333 4.667 6.733 6.933 6.467 4.533 3.600 2.733 5.467 3.800 3.467 6.467 5.533 4.133
print(sort(ratings, decreasing=TRUE), digits=4)
# 4 8 7 9 16 5 1 17 13 3 6 10 18 2 14 11 15 12
# 7.667 6.933 6.733 6.467 6.467 6.333 5.533 5.533 5.467 4.800 4.667 4.533 4.133 3.933 3.800 3.600 3.467 2.733
So combination 4 is the top rated and combination 12 is the lowest rated. If you want to add more descriptive information:
attribs <- colnames(tv_data)[2:12]
present <- as.matrix(tv_data[1:18, 2:12]) > 0
labels <- t(apply(x, 1, function(x) attribs[x]))
results <- data.frame(Comb=1:18, ratings, labels)
results
# Comb ratings X1 X2 X3 X4
# 1 1 5.533333 panel_lcd res_4k tam_65 p_1500
# 2 2 3.933333 panel_lcd res_4k tam_55 p_1500
# 3 3 4.800000 panel_oled res_4k tam_42 p_1500
# 4 4 7.666667 panel_oled res_4k tam_65 p_1000
# 5 5 6.333333 panel_qled res_4k tam_55 p_1000
# 6 6 4.666667 panel_oled res_4k tam_42 p_1000
# 7 7 6.733333 panel_lcd res_4k tam_65 p_500
# 8 8 6.933333 panel_oled res_4k tam_55 p_500
# 9 9 6.466667 panel_qled res_4k tam_42 p_500
# 10 10 4.533333 panel_oled res_hd tam_65 p_1500
# 11 11 3.600000 panel_qled res_hd tam_55 p_1500
# 12 12 2.733333 panel_qled res_hd tam_42 p_1500
# 13 13 5.466667 panel_qled res_hd tam_65 p_1000
# 14 14 3.800000 panel_lcd res_hd tam_55 p_1000
# 15 15 3.466667 panel_lcd res_hd tam_42 p_1000
# 16 16 6.466667 panel_qled res_hd tam_65 p_500
# 17 17 5.533333 panel_oled res_hd tam_55 p_500
# 18 18 4.133333 panel_lcd res_hd tam_42 p_500

Factor analysis not assigning to object

I am running an exploratory factor analysis and am specifying that the factor analysis results be loaded onto f2.
My code is below. The first line runs but when I go to run my next line of code, where I reference f2, I get a message "object 'f2' not found".
So somehow the assignment isn't working. Am I missing something really obvious? The factor analysis uses the psych package. I have checked the documentation and the examples they give assign factor analysis results to objects similarly, so I'm a bit stumped. Any pointers would be greatly appreciated.
f2 <- fa(MDI, nfactors = 30, n.obs = 286, rotation = "promax", min.err = 0.001,
fm = "wls", cor = "poly")
load = loadings(f2)
print(load, sort = TRUE, digits = 2, cutoff = 0.001)
plot(load)
identify(load, labels = names(MDI))
plot(f2, labels = names(MDI))
Below is dput of the correlation matrix so it can be reproducible.
> dput(MDI)
structure(list(X = structure(c(1L, 12L, 23L, 25L, 26L, 27L, 28L,
29L, 30L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 24L), .Label = c("mdi1",
"mdi10", "mdi11", "mdi12", "mdi13", "mdi14", "mdi15", "mdi16",
"mdi17", "mdi18", "mdi19", "mdi2", "mdi20", "mdi21", "mdi22",
"mdi23", "mdi24", "mdi25", "mdi26", "mdi27", "mdi28", "mdi29",
"mdi3", "mdi30", "mdi4", "mdi5", "mdi6", "mdi7", "mdi8", "mdi9"
), class = "factor"), mdi1 = c(1, 0.3936173, 0.4689895, 0.3723157,
0.5000249, 0.4007723, 0.4752565, 0.3299713, 0.4467554, 0.3092035,
0.4587867, 0.3502716, 0.4765846, 0.4296331, 0.4279444, 0.3978138,
0.3130349, 0.4217768, 0.4217768, 0.3941672, 0.442678, 0.4143811,
0.4883476, 0.3818616, 0.4253592, 0.4220679, 0.3341886, 0.2904774,
0.4125257, 0.471747), mdi2 = c(0.3936173, 1, 0.5687221, 0.5503958,
0.3579519, 0.5524491, 0.4113922, 0.5692459, 0.5244077, 0.4519596,
0.4691271, 0.3328134, 0.3909096, 0.5207473, 0.517872, 0.4460308,
0.5357907, 0.5271745, 0.5656711, 0.6416844, 0.5933588, 0.5301733,
0.4632707, 0.3692169, 0.3899222, 0.6722428, 0.571866, 0.4770556,
0.4447538, 0.4801506), mi3 = c(0.4689895, 0.5687221, 1, 0.5337568,
0.4605771, 0.5801225, 0.4748034, 0.5027398, 0.6493962, 0.4371995,
0.4814276, 0.5174168, 0.4575024, 0.5161417, 0.7302354, 0.443922,
0.4503891, 0.6403537, 0.4638545, 0.5755256, 0.6559644, 0.5669309,
0.4882669, 0.540937, 0.4305827, 0.6208696, 0.5774123, 0.4337106,
0.5007241, 0.5812161), mdi4 = c(0.3723157, 0.5503958, 0.5337568,
1, 0.4647914, 0.5432398, 0.5222107, 0.480119, 0.5095777, 0.6127156,
0.5411, 0.3438971, 0.5132041, 0.4708869, 0.5773365, 0.640864,
0.5419371, 0.4278067, 0.4294387, 0.5762886, 0.6173602, 0.696368,
0.4768973, 0.4312955, 0.4800561, 0.4966285, 0.4940535, 0.6449681,
0.4448795, 0.4511662), mdi5 = c(0.5000249, 0.3579519, 0.4605771,
0.4647914, 1, 0.529076, 0.4627546, 0.4322669, 0.5266816, 0.4193437,
0.5274925, 0.355744, 0.4748984, 0.3943626, 0.5452649, 0.4282942,
0.3884295, 0.4405988, 0.5013351, 0.4372788, 0.5033538, 0.449599,
0.4719872, 0.5221968, 0.4572666, 0.478281, 0.4474777, 0.436465,
0.6423572, 0.5367758), mdi6 = c(0.4007723, 0.5524491, 0.5801225,
0.5432398, 0.529076, 1, 0.5481986, 0.5292259, 0.554901, 0.5197566,
0.6618723, 0.7776074, 0.5538152, 0.5513789, 0.6005339, 0.5479274,
0.5154317, 0.7863542, 0.5508043, 0.6893594, 0.5754793, 0.6092226,
0.5590816, 0.7415316, 0.5702535, 0.7306869, 0.6161738, 0.5381451,
0.5487092, 0.7739138), mdi7 = c(0.4752565, 0.4113922, 0.4748034,
0.5222107, 0.4627546, 0.5481986, 1, 0.4208138, 0.5818144, 0.5123467,
0.5154297, 0.5182619, 0.6342495, 0.5054629, 0.5154232, 0.550466,
0.4921495, 0.4687262, 0.4268752, 0.56955, 0.5046245, 0.5563658,
0.544639, 0.4961694, 0.6133689, 0.5084869, 0.4299369, 0.5220904,
0.4294706, 0.4739928), mdi8 = c(0.3299713, 0.5692459, 0.5027398,
0.480119, 0.4322669, 0.5292259, 0.4208138, 1, 0.5723092, 0.3119612,
0.4675261, 0.3380419, 0.353584, 0.4541021, 0.5421594, 0.474245,
0.6079999, 0.5235899, 0.5335896, 0.4957257, 0.5442768, 0.4974862,
0.5023641, 0.4025193, 0.4116167, 0.5727691, 0.6270551, 0.4208924,
0.5064872, 0.4869765), mdi9 = c(0.4467554, 0.5244077, 0.6493962,
0.5095777, 0.5266816, 0.554901, 0.5818144, 0.5723092, 1, 0.490689,
0.5181651, 0.4850319, 0.5356553, 0.6158338, 0.6559043, 0.5307519,
0.5871398, 0.5846594, 0.5470594, 0.6284931, 0.6879265, 0.6163574,
0.512804, 0.5047779, 0.5466345, 0.613476, 0.5883362, 0.506302,
0.4979364, 0.5808664), mdi10 = c(0.3092035, 0.4519596, 0.4371995,
0.6127156, 0.4193437, 0.5197566, 0.5123467, 0.3119612, 0.490689,
1, 0.5118569, 0.4462334, 0.4532171, 0.4315306, 0.4522421, 0.7588315,
0.4401081, 0.4350122, 0.4499751, 0.5206525, 0.5352926, 0.6998865,
0.4755847, 0.4100334, 0.4787971, 0.453012, 0.4734884, 0.7813307,
0.535401, 0.4395691), mdi11 = c(0.4587867, 0.4691271, 0.4814276,
0.5411, 0.5274925, 0.6618723, 0.5154297, 0.4675261, 0.5181651,
0.5118569, 1, 0.5527827, 0.501612, 0.5149324, 0.540138, 0.5069535,
0.528538, 0.579202, 0.5901564, 0.5299329, 0.5250493, 0.5154014,
0.5127527, 0.5788864, 0.5161334, 0.507289, 0.5158013, 0.5344951,
0.5978617, 0.647102), mdi12 = c(0.3502716, 0.3328134, 0.5174168,
0.3438971, 0.355744, 0.7776074, 0.5182619, 0.3380419, 0.4850319,
0.4462334, 0.5527827, 1, 0.5154903, 0.4620118, 0.472202, 0.4547294,
0.4840607, 0.8285936, 0.5358637, 0.5490359, 0.4616615, 0.4249117,
0.4808445, 0.7267043, 0.552395, 0.5925844, 0.4751664, 0.3929154,
0.4023344, 0.6972608), mdi13 = c(0.4765846, 0.3909096, 0.4575024,
0.5132041, 0.4748984, 0.5538152, 0.6342495, 0.353584, 0.5356553,
0.4532171, 0.501612, 0.5154903, 1, 0.517566, 0.5669901, 0.4811508,
0.4159442, 0.5916418, 0.5317423, 0.6019275, 0.5150589, 0.6103914,
0.7036702, 0.5396378, 0.7106897, 0.5663952, 0.4743636, 0.5357141,
0.4997759, 0.5310849), mdi14 = c(0.4296331, 0.5207473, 0.5161417,
0.4708869, 0.3943626, 0.5513789, 0.5054629, 0.4541021, 0.6158338,
0.4315306, 0.5149324, 0.4620118, 0.517566, 1, 0.5294991, 0.4641652,
0.5595265, 0.5568292, 0.5329007, 0.6300983, 0.5476386, 0.6433976,
0.5124435, 0.5178623, 0.4634521, 0.6025023, 0.4958374, 0.4741311,
0.4363587, 0.5922855), mdi15 = c(0.4279444, 0.517872, 0.7302354,
0.5773365, 0.5452649, 0.6005339, 0.5154232, 0.5421594, 0.6559043,
0.4522421, 0.540138, 0.472202, 0.5669901, 0.5294991, 1, 0.4969139,
0.5407898, 0.711619, 0.5776979, 0.6465131, 0.8102714, 0.6210396,
0.5843541, 0.5708951, 0.5353925, 0.6430862, 0.6773275, 0.5256203,
0.5525755, 0.5968413), mdi16 = c(0.3978138, 0.4460308, 0.443922,
0.640864, 0.4282942, 0.5479274, 0.550466, 0.474245, 0.5307519,
0.7588315, 0.5069535, 0.4547294, 0.4811508, 0.4641652, 0.4969139,
1, 0.5327385, 0.4919386, 0.4640105, 0.6189401, 0.6221125, 0.7849677,
0.5263167, 0.4961512, 0.546515, 0.4792591, 0.5387354, 0.7593365,
0.5443296, 0.4965095), mdi17 = c(0.3130349, 0.5357907, 0.4503891,
0.5419371, 0.3884295, 0.5154317, 0.4921495, 0.6079999, 0.5871398,
0.4401081, 0.528538, 0.4840607, 0.4159442, 0.5595265, 0.5407898,
0.5327385, 1, 0.5972664, 0.6531257, 0.6124992, 0.6593416, 0.5779083,
0.54295, 0.5091556, 0.4999436, 0.5534753, 0.6906962, 0.5515724,
0.5763667, 0.5257787), mdi18 = c(0.4217768, 0.5271745, 0.6403537,
0.4278067, 0.4405988, 0.7863542, 0.4687262, 0.5235899, 0.5846594,
0.4350122, 0.579202, 0.8285936, 0.5916418, 0.5568292, 0.711619,
0.4919386, 0.5972664, 1, 0.616892, 0.6776108, 0.653145, 0.6462477,
0.6133169, 0.76014, 0.5693621, 0.7255007, 0.6772965, 0.4731968,
0.6136029, 0.8215128), mdi19 = c(0.4217768, 0.5656711, 0.4638545,
0.4294387, 0.5013351, 0.5508043, 0.4268752, 0.5335896, 0.5470594,
0.4499751, 0.5901564, 0.5358637, 0.5317423, 0.5329007, 0.5776979,
0.4640105, 0.6531257, 0.616892, 1, 0.6277485, 0.5888859, 0.5781749,
0.5962161, 0.4966702, 0.525626, 0.5893053, 0.5937545, 0.4536458,
0.6023978, 0.595442), mdi20 = c(0.3941672, 0.6416844, 0.5755256,
0.5762886, 0.4372788, 0.6893594, 0.56955, 0.4957257, 0.6284931,
0.5206525, 0.5299329, 0.5490359, 0.6019275, 0.6300983, 0.6465131,
0.6189401, 0.6124992, 0.6776108, 0.6277485, 1, 0.7456018, 0.7237394,
0.5914482, 0.6174089, 0.6084753, 0.7583413, 0.6222094, 0.6093335,
0.5607048, 0.6734581), mdi21 = c(0.442678, 0.5933588, 0.6559644,
0.6173602, 0.5033538, 0.5754793, 0.5046245, 0.5442768, 0.6879265,
0.5352926, 0.5250493, 0.4616615, 0.5150589, 0.5476386, 0.8102714,
0.6221125, 0.6593416, 0.653145, 0.5888859, 0.7456018, 1, 0.6836611,
0.5767896, 0.5020915, 0.5302819, 0.6788499, 0.7302629, 0.5532899,
0.514876, 0.5369505), mdi22 = c(0.4143811, 0.5301733, 0.5669309,
0.696368, 0.449599, 0.6092226, 0.5563658, 0.4974862, 0.6163574,
0.6998865, 0.5154014, 0.4249117, 0.6103914, 0.6433976, 0.6210396,
0.7849677, 0.5779083, 0.6462477, 0.5781749, 0.7237394, 0.6836611,
1, 0.6970878, 0.5551017, 0.5309829, 0.669629, 0.618117, 0.7725701,
0.5368407, 0.6161932), mdi23 = c(0.4883476, 0.4632707, 0.4882669,
0.4768973, 0.4719872, 0.5590816, 0.544639, 0.5023641, 0.512804,
0.4755847, 0.5127527, 0.4808445, 0.7036702, 0.5124435, 0.5843541,
0.5263167, 0.54295, 0.6133169, 0.5962161, 0.5914482, 0.5767896,
0.6970878, 1, 0.5780347, 0.5722519, 0.6007872, 0.6153544, 0.579486,
0.5826146, 0.5400445), mdi24 = c(0.3818616, 0.3692169, 0.540937,
0.4312955, 0.5221968, 0.7415316, 0.4961694, 0.4025193, 0.5047779,
0.4100334, 0.5788864, 0.7267043, 0.5396378, 0.5178623, 0.5708951,
0.4961512, 0.5091556, 0.76014, 0.4966702, 0.6174089, 0.5020915,
0.5551017, 0.5780347, 1, 0.5823804, 0.6163705, 0.5893418, 0.4599374,
0.4879448, 0.7233099), mdi25 = c(0.4253592, 0.3899222, 0.4305827,
0.4800561, 0.4572666, 0.5702535, 0.6133689, 0.4116167, 0.5466345,
0.4787971, 0.5161334, 0.552395, 0.7106897, 0.4634521, 0.5353925,
0.546515, 0.4999436, 0.5693621, 0.525626, 0.6084753, 0.5302819,
0.5309829, 0.5722519, 0.5823804, 1, 0.6287262, 0.4351833, 0.541565,
0.488426, 0.5750416), mdi26 = c(0.4220679, 0.6722428, 0.6208696,
0.4966285, 0.478281, 0.7306869, 0.5084869, 0.5727691, 0.613476,
0.453012, 0.507289, 0.5925844, 0.5663952, 0.6025023, 0.6430862,
0.4792591, 0.5534753, 0.7255007, 0.5893053, 0.7583413, 0.6788499,
0.669629, 0.6007872, 0.6163705, 0.6287262, 1, 0.6485229, 0.6028357,
0.5814636, 0.6474577), mdi27 = c(0.3341886, 0.571866, 0.5774123,
0.4940535, 0.4474777, 0.6161738, 0.4299369, 0.6270551, 0.5883362,
0.4734884, 0.5158013, 0.4751664, 0.4743636, 0.4958374, 0.6773275,
0.5387354, 0.6906962, 0.6772965, 0.5937545, 0.6222094, 0.7302629,
0.618117, 0.6153544, 0.5893418, 0.4351833, 0.6485229, 1, 0.5630586,
0.5338222, 0.5340758), mdi28 = c(0.2904774, 0.4770556, 0.4337106,
0.6449681, 0.436465, 0.5381451, 0.5220904, 0.4208924, 0.506302,
0.7813307, 0.5344951, 0.3929154, 0.5357141, 0.4741311, 0.5256203,
0.7593365, 0.5515724, 0.4731968, 0.4536458, 0.6093335, 0.5532899,
0.7725701, 0.579486, 0.4599374, 0.541565, 0.6028357, 0.5630586,
1, 0.5835477, 0.5081215), mdi29 = c(0.4125257, 0.4447538, 0.5007241,
0.4448795, 0.6423572, 0.5487092, 0.4294706, 0.5064872, 0.4979364,
0.535401, 0.5978617, 0.4023344, 0.4997759, 0.4363587, 0.5525755,
0.5443296, 0.5763667, 0.6136029, 0.6023978, 0.5607048, 0.514876,
0.5368407, 0.5826146, 0.4879448, 0.488426, 0.5814636, 0.5338222,
0.5835477, 1, 0.611525), mdi30 = c(0.471747, 0.4801506, 0.5812161,
0.4511662, 0.5367758, 0.7739138, 0.4739928, 0.4869765, 0.5808664,
0.4395691, 0.647102, 0.6972608, 0.5310849, 0.5922855, 0.5968413,
0.4965095, 0.5257787, 0.8215128, 0.595442, 0.6734581, 0.5369505,
0.6161932, 0.5400445, 0.7233099, 0.5750416, 0.6474577, 0.5340758,
0.5081215, NA, 1)), class = "data.frame", row.names = c(NA, -30L
))
I was able to use the raw data as input for MDI and the factor analysis ran with error messages (in comment below so that this post isn't super long). The actual dataset is really big but I created a smaller dataset from it that generates the same error message when I run the factor analysis. THe dput for that smaller data set is below.
>dput(MDI)
structure(list(MDIdisengagement = c(2L, 1L, 1L, 0L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 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 1L, 1L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 4L, 0L, 2L, 0L, 0L,
0L, 3L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
3L, 0L, 0L, 0L, 0L, 4L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 1L, 2L, 1L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 1L, 1L,
0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 7L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L,
0L, 1L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 7L, 0L, 0L, 1L, 1L, 0L, 0L,
0L, 4L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L,
2L, 0L, 0L, 3L, 1L, 0L, 0L, 4L, 0L, 0L, 0L, 0L, 4L, 0L, 0L, 2L,
0L, 0L)), class = "data.frame", row.names = c(NA, -291L))
You have a few problems going on in your call to fa. First you need to remove the X column to provide a proper correlation matrix. This may not be an issue for you but of course is for your dput example. However even after fixing this the main issue is with your call to polychoric correlation. If you want to specify cor = "poly" you need to use the discrete data.
What I would suggest is to run your call to fa using the discrete data with only the measurement variables and no extra variables like X. If you are still getting errors then please update your example to include the discrete data and I can try to help you.
EDIT: So now your big problem is that you only have 6 items and in factor analysis you usually want a minimum of three items per factor which in your case would leave you with at most 2 factors but even that is pushing it. With nfactors=1 it will work but not with nfactors>1 To see why you are having issues you can look at the eigenvalues of your correlation matrix like so:
eigen(polychoric(MDI)$rho)
This produces the following output:
$values
[1] 4.76730952 0.68445132 0.28440838 0.18855401 0.05141578 0.02386099
As you can see, nearly all of the variance is being explained by the first factor and there is little left after. Therefore, it is not surprising that you are getting errors when trying to extract more than one factor. A scree plot will visually show this.
So this will work but the results are probably not very interesting:
f2 <- fa(MDI,nfactors=1,rotation="promax", min.err= 0.001, fm="wls", cor="poly")
Does that make sense?

SomersDelta: Error in as.table.default(x) : cannot coerce to a tabl in R

data example
sommer=structure(list(tub = c(1L, 2L, 0L, 2L, 0L, 0L, 0L, 0L, 0L,
1L, 1L, 2L, 1L, 0L, 0L, 1L, 0L, 2L, 1L, 1L, 0L, 0L, 1L, 0L, 0L,
2L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 3L, 0L, 1L, 1L, 1L, 1L,
0L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 3L,
2L, 0L, 1L, 0L, 3L, 2L, 2L, 0L, 0L, 0L, 1L, 0L, 0L, 3L, 1L, 1L,
3L, 1L), fq = c(1L, 1L, 0L, 1L, 0L, 0L, 0L, 0L,
0L, 1L, 3L, 6L, 1L, 0L, 0L, 1L, 0L, 2L, 4L, 2L, 0L, 0L, 2L, 0L,
0L, 5L, 0L, 1L, 2L, 0L, 0L, 1L, 0L, 0L, 0L, 14L, 0L, 1L, 1L,
1L, 4L, 0L, 1L, 0L, 4L, 0L, 0L, 0L, 6L, 1L, 0L, 0L, 1L, 1L, 0L,
1L, 3L, 4L, 0L, 1L, 0L, 8L, 1L, 2L, 0L, 0L, 0L, 2L, 0L, 0L, 3L,
2L, 2L, 1L, 1L)), .Names = c("tub", "fq"), class = "data.frame", row.names = c(NA,
-75L))
i want calculate SomersDelta
library("DescTools")
SomersDelta(sommer, direction = c("row", "column"))
and i get the error
Error in as.table.default(x) : cannot coerce to a table
also i want get spine plot
library("coin")
spineplot(sommer)
but i get the error
Error in margin.table(tab, 1) : 'x' is not an array
are there two errors interconnection?
How to calculate SomersDelta and get spine plot like this
spine plot
It requires a matrix as input. According to ?SomersDelta
x - a numeric vector or a table. A matrix will be treated as table.
Here, the dataset is a data.frame class. We can convert it to matrix (as.matrix) and it should work fine
SomersDelta(as.matrix(sommer), direction = c("row", "column"))
#[1] -0.06137931

Heatmap with multiple/stacked variables per individual

I am trying to create a heatmap. Each individual has three binary variables (ecz, whz, rhi) across 6 time points. The attached heatmap isn't at all informative as I want to see how the variables develop concurrently.
For each individual rows), I would like 3 rows per individual at each time point with 3 different colours to represent each symptom. The columns would represent each of the 6 time points.
I would be most grateful for any help!
Here is my data:
structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L,
26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L,
39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 0L,
1L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 1L,
1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 0L,
0L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 0L,
1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 0L,
1L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L,
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0L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L,
0L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L,
1L, 0L, 1L), .Dim = c(50L, 19L), .Dimnames = list(NULL, c("idno",
"eczms1", "whzms1", "rhims1", "eczms3", "whzms3", "rhims3", "eczms5",
"whzms5", "rhims5", "eczms8", "whzms8", "rhims8", "eczms11",
"whzms11", "rhims11", "eczms16", "whzms16", "rhims16")))
Here is the code for the attached heatmap:
library(pheatmap)
am=as.matrix(am)
col = c("darkgreen","red")
breaks <- c(-1, 0, 1)
c1=pheatmap(am, show_rownames=FALSE, col=col, cluster_rows = FALSE,
cluster_cols=FALSE, legend =TRUE,
legend_breaks = 0:1, legend_labels = c("No","Yes"))
I have a solution using the tidyverse packages:
EDIT:
Reordered idno so that patients are sorted by appearance of ecz.
library(tidyverse)
input <- structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L,
26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L,
39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 0L,
1L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 1L,
1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 0L,
0L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 0L,
1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 0L,
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0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L,
0L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L,
0L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L,
1L, 0L, 1L), .Dim = c(50L, 19L), .Dimnames = list(NULL, c("idno",
"eczms1", "whzms1", "rhims1", "eczms3", "whzms3", "rhims3", "eczms5",
"whzms5", "rhims5", "eczms8", "whzms8", "rhims8", "eczms11",
"whzms11", "rhims11", "eczms16", "whzms16", "rhims16")))
tidy <- tibble::as.tibble(input) %>%
tidyr::gather(key = "key", value = "value", -idno) %>%
tidyr::separate(key, into = c("symptom", "time"), sep = "(?<=[A-Za-z])(?=[0-9])") %>%
dplyr::mutate(
time = as.factor(as.numeric(time)),
value = ifelse(value, symptom, NA),
symptom = factor(symptom, levels = c("eczms", "whzms", "rhims"), labels = c("ecz", "whz", "rhi")),
idno = factor(idno, levels = unique(idno[order(value)]))
)
ggplot(tidy, aes(x = time, y = symptom, fill = value)) +
geom_raster() +
facet_wrap(~idno) +
scale_fill_brewer(guide = FALSE, palette = "Set2")
#> Warning: Removed 474 rows containing missing values (geom_raster).
ggplot(tidy, aes(x = time, y = reorder(idno, dplyr::desc(idno)), fill = value)) +
geom_raster() +
facet_wrap(~symptom) +
scale_fill_brewer(guide = FALSE, palette = "Set2")
#> Warning: Removed 474 rows containing missing values (geom_raster).
Created on 2018-11-09 by the reprex package (v0.2.1)

plotting mean of variable versus matrix of conditions in R using ggplot2

I have a data.frame X with column X and a data.frame C with M binary values (0/1). Both data.frames have N rows (examples).
I would like to average X on each case 0/1 of each m out of M column of C.
When I plot this, I accept to get M*2 bars where x axis are the column names of each column in C and red/blue is for when catergory m (out of M) is either 0/1.
Can this be done using ggplot2?
Any other quick way to do that without for loops?
Result sketch:
*
* * *
* * * *
m1=0, m1=1, m2=0, m2=1 ,....
Thanks,
Hanan
data sample below:
aggregate(X, by = as.list(C), FUN=mean) will aggregate to any combination of C. This is not what I want. I want X aggregated for every value of each column of C INDEPENDENTLY .
X<-structure(list(V1 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L,
0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)),
.Names = "V1", class = "data.frame", row.names = c(NA, -100L))
C<-structure(list(V1 = c(1L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 1L, 1L,
0L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L,
0L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 0L,
1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L,
1L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 1L,
1L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 0L,
1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L), V2 = c(1L, 0L, 1L, 0L,
1L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L,
1L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L,
1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 1L,
0L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L,
0L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L
), V3 = c(1L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 1L,
1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 1L,
0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L,
1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L,
1L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L,
0L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L,
1L, 1L, 0L, 0L, 1L, 1L, 1L)),
.Names = c("V1", "V2", "V3"), class = "data.frame", row.names = c(NA, -100L))
Here is a way to transform your data broken down by incremental steps
dd <- do.call(rbind,
Map(function(a,b) cbind(C=a, b), names(C),
lapply(
lapply(
lapply(C, table, X[[1]], dnn=c("CV","X")),
as.data.frame),
subset, X==1)
))
So here we use table() to get the counts of each X value for each C value. Then we turn that into a data.frame and take only the counts for X=1. Finally we add the correct name of the C column and merge all the data.frames into one large data.frame.
Then we can plot that with
ggplot(dd, aes(x=C, y=Freq, fill=CV)) +
geom_bar(position="dodge", stat="identity")
So the columns of C are listed along the x-axis and the values of C are represented by the color of the bar. The counts of X=1 in each of the groups are the heights of the bars.

Resources