Remove empty list from a list of lists - r

I have a list of lists where some of them are NA e.g. empty lists. I want to extract all the lists which are filled with data and remove all the lists which are empty(NA).
The code i'm trying is:
lapply(outputfile,function(x){
if(outputfile != NA){
test<-lapply(outputfile,unlist)
}})
But this does not work.
The list of lists is like this: (small example of random data)
list(NA, NA, NA, NA, NA, NA, list(c(5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5)))
I only want to extract the list with the 5s in it. The first 6 lists should be ignored e.g. removed.
Any help is appreciated

So, to remove NA at the first level, you could use is.na directly:
l[!is.na(l)]
Alternatively, you can also use Filter which tries to coerce the results of the evaluated function to logical and returns those elements that evaluated to TRUE. You could do, for example:
Filter(function(x) !is.na(x), l)
(or) equivalently (as #flodel writes under comment)
Filter(Negate(is.na), l)

Related

R: item name missing in the plot legend

With this code I get the plot I want
d <- density(mydata$item1)
plot(d)
This code is the same, but omits N/As. And there is a flaw in the plot's legend. As you can see, it doesn't tell what item is plotted, (x = .)
Can you tell where is the matter and how to fix it? Thank you for your help.
My data
structure(list(item1 = c(5, 5, 5, 5, 4, 4, 2, 1, 3, 4, 4, 3,
2, 5, 2, 4, 4, 3, 6, 5, 3, 2, 5, 3, 3, 1, 3, 5, 1, 3, 2, 6, 3,
5, 4, 4, 3, 5, 6, 3, 2, 6, 6, 5, 2, 2, 2, 3, 3, 3), item2 = c(5,
4, 5, 1, 2, 2, 3, 2, 2, 2, 2, 3, 2, 5, 1, 4, 4, 3, 3, 5, 3, 2,
4, 4, 3, 4, 4, 3, 7, NA, 2, 4, 2, 4, 2, 3, 5, 3, 5, 3, 2, 6,
6, 7, 2, 3, 2, 3, 1, 4), item3 = c(5, 5, 6, 7, 3, 4, 5, 2, 2,
6, 4, 2, 5, 7, 1, 2, 4, 5, 6, 6, 5, 2, 6, 5, 6, 4, 6, 4, 6, 4,
6, 5, 5, 6, 6, 6, 5, 6, 7, 5, 5, 7, 7, 6, 2, 6, 6, 6, 5, 3)), row.names = c(NA,
-50L), class = c("tbl_df", "tbl", "data.frame"))
Use the main = argument inside plot to make the title say whatever you want it to.
Data$item2 %>%
na.omit() %>%
density() %>%
plot(main = 'Density of Data$item2')
you had a little typo in your code as the density() call was piped into a plot call refering to the variable it was been written to ... this might have resulted in the strange plot.
In general the density() function won't work with NA values acording to the documentation so you have to set the argument na.rm = TRUE as the default is FALSE for the plot to work correctly... also as #AllanCameron pointed out in an earlier answer you can set the plot title manually.
d <- density(mydata$item2, na.rm = TRUE)
plot(d)
Possibly you can substitute, interpolate or impute the NA values so that you do not have to remove them for the denstiy() call. Though this obviously depends on your data, context and goals.

R: How to plot a boxplot with numeric x-axis for according spacing (not ggplot)

I want to plot a boxplot with the regular /boxplot function of R - not ggplot.
Y and X axis are continously numeric varibales (x-axis 6 forces: 1.0, 1.3, 1.6, 2.0, 2.5, 3.1 [N])
On Y-axis the participants ratings (1 to 7).
I would like to plot it, with quantified spacing on x-axis, and also later add regression line into the plot. Cant find anything for the regular /boxplot function.
Code so far:
kraft_ou <- data.frame(VR1_100$ou_kraft,
VR1_125$ou_kraft,
VR1_160$ou_kraft,
VR1_200$ou_kraft,
VR1_250$ou_kraft,
VR1_310$ou_kraft)
colnames(kraft_ou) <- c("kraft_100", "kraft_125", "kraft_160", "kraft_200", "kraft_250", "kraft_310")
kraft_ou
boxplot(kraft_ou,
names=c("1,0 [N]", "1,3 [N]","1,6 [N]","2,0 [N]","2,5 [N]","3,1 [N]"),
col = "bisque",
ylim = c(1, 7))
points(1:6, meanskraftou, pch=4)
text(1:6, meanskraftou + 0.24, labels = meanskraftou)
abline(h=4)
data (n=30 ratings from 1 to 7 for each of the 6 forces):
dput(kraft_ou)
structure(list(kraft_100 = c(4, 3, 5, 5, 3, 4, 2, 4, 4, 5, 4,
5, 5, 4, 4, 3, 4, 4, 5, 4, 6, 5, 4, 5, 5, 5, 4, 4, 4, 4), kraft_125 = c(4,
4, 5, 6, 4, 3, 4, 4, 4, 5, 4, 5, 4, 5, 4, 3, 4, 4, 4, 6, 6, 4,
4, 5, 3, 5, 4, 4, 4, 5), kraft_160 = c(5, 6, 6, 6, 6, 4, 6, 5,
6, 5, 4, 3, 6, 6, 6, 5, 5, 5, 5, 6, 6, 6, 5, 5, 4, 6, 4, 5, 5,
5), kraft_200 = c(6, 5, 6, 6, 5, 4, 5, 5, 6, 7, 5, 3, 5, 5, 5,
4, 7, 6, 5, 5, 7, 6, 5, 6, 6, 6, 5, 4, 5, 3), kraft_250 = c(5,
6, 6, 7, 6, 6, 6, 6, 7, 7, 6, 5, 7, 7, 5, 5, 6, 6, 7, 7, 6, 6,
5, 5, 5, 7, 4, 6, 6, 5), kraft_310 = c(7, 7, 7, 7, 6, 5, 6, 6,
6, 7, 4, 5, 7, 6, 5, 5, 7, 6, 5, 6, 6, 6, 5, 6, 5, 6, 5, 6, 6,
6)), class = "data.frame", row.names = c(NA, -30L))
You can use the at argument to specify x locations for your boxplots, though to get them narrow enough to avoid overplotting, you need to add an invisible box and set the relative widths of the visible boxes to a smaller value:
boxplot(cbind(kraft_ou, n = rep(NA, nrow(kraft_ou))),
names=c("1,0 [N]", "1,3 [N]","1,6 [N]","2,0 [N]","2,5 [N]","3,1 [N]",
" "),
col = "bisque",
ylim = c(1, 7), width = c(0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 1),
at = c(1, 1.3, 1.6, 2.0, 2.5, 3.1, 3.1))
abline(h = 4)
To add a regression line, you would need to have all your data frame values in a single y variable, and a vector of their corresponding x axis positions:
abline(lm(unlist(kraft_ou) ~ rep(c(1, 1.3, 1.6, 2.0, 2.5, 3.1), each = 30)))

Difference between fit$loadings and fit$Vaccounted for variance accounted for in factor analysis?

I am getting different values for variance accounted for by factors in factor analysis whether I check them with fit$loadings or with fit$Vaccounted. I am using the psych package with the fa() function. Why would that be the case if they're supposed to be exactly the same thing (I guess they're not or that they are calculated differently)?
The total difference is not huge, but still not trivial (about 0.7 for cumulative). I have a reprex below.
(I'm sorry for the large dataset, I was not able to replicate the issue with different datasets or a subset, so it might have to do with something funky with the data.)
data <- structure(list(X1 = c(5, 5, 5, 7, 2, 2, 2, 2, 7, 5, 4, 9, 8,
8, 6, 9, 9, 2, 2, 2, 2, 3, 2, 2, 9, 7, 8, 4, 3, 4, 6, 6, 3, 4,
4, 4, 8, 7, 6, 7, 5, 6, 6, 4, 8, 8, 8, 3, 9, 9, 6, 4, 8, 7, 8,
7, 8, 8, 8, 8), X2 = c(6, 4, 4, 6, 2, 2, 2, 2, 6, 5, 4, 8, 7,
9, 6, 9, 4, 2, 2, 2, 6, 4, 6, 7, 9, 6, 8, 4, 3, 3, 5, 5, 2, 3,
4, 7, 7, 5, 5, 6, 7, 7, 7, 3, 8, 5, 3, 2, 9, 9, 4, 4, 4, 6, 4,
4, 8, 8, 8, 8), X3 = c(7, 5, 4, 7, 2, 2, 2, 2, 7, 5, 3, 7, 8,
9, 7, 9, 2, 2, 2, 2, 4, 2, 5, 4, 9, 6, 8, 4, 3, 2, 4, 5, 3, 2,
2, 7, 7, 6, 6, 5, 7, 7, 7, 4, 8, 7, 3, 2, 9, 9, 4, 3, 4, 4, 5,
5, 8, 7, 7, 7), X5 = c(7, 6, 4, 6, 2, 2, 2, 2, 6, 4, 3, 7, 7,
9, 6, 9, 2, 2, 2, 2, 2, 2, 4, 4, 9, 8, 6, 5, 2, 2, 4, 3, 2, 2,
4, 7, 7, 6, 5, 6, 7, 7, 7, 3, 4, 5, 3, 2, 9, 9, 4, 2, 4, 4,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 3, 3, 3, 2, 4, 2, 3, 3, 2, 2, 6, 8,
7, 8, 8, 2, 2, 2, 2, 2, 2, 2, 2, 8, 5, 8, 2, 2, 2, 2, 2, 3, 2,
2, 3, 2, 6, 4, 6, 9, 9, 9, 5, 2, 9, 2, 2, 5, 4, 6, 7, 2, 2, 2,
2, 5, 6, 5, 6), X34 = c(2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 4,
3, 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2,
6, 6, 2, 2, 3, 2, 6, 8, 7, 2, 2, 2, 3, 2, 6, 4, 3, 3, 3, 4, 3,
3, 4, 3, 4, 2)), class = "data.frame", row.names = c(NA, 60L))
Now that we defined the dataset, let's jump in with the code.
library(psych)
fit <- fa(data, nfactors = 4)
#> Loading required namespace: GPArotation
print(fit$loadings)
#>
#> [Loadings truncated for brevity]
#>
#> MR1 MR2 MR3 MR4
#> SS loadings 9.464 3.571 2.171 1.682
#> Proportion Var 0.338 0.128 0.078 0.060
#> Cumulative Var 0.338 0.466 0.543 0.603
print(fit$Vaccounted, digits = 3)
#> MR1 MR2 MR3 MR4
#> SS loadings 10.392 4.328 2.324 1.8283
#> Proportion Var 0.371 0.155 0.083 0.0653
#> Cumulative Var 0.371 0.526 0.609 0.6740
Created on 2022-02-10 by the reprex package (v2.0.1)
We can see the values differ. Any ideas why?
https://www.researchgate.net/post/How_can_of_Variance_of_factors_in_exploratory_factor_analysis_be_calculated_when_factors_are_correlated
I am not familiar with factor analysis, but as shown here, it seems that SS loading cannot be calculated as a sum of squares because of inter-factor correlations when oblique rotation is used. Perhaps, fit$Vaccounted takes this problem into account but fit$loadings is simply the sum of squares. I think this difference appears.
Note that the default rotation in the fa package is oblimin which is obliqu rotation, so I think this difference will appear.

Troubleshooting R table function to create a more complete-looking table

I'm attempting to create a table of relative percentage values for answers to a questionnaire (answers are graded 1-5) for a total of 3 questions.
I'm using the formattable library to convert the values in the tables to percents, but thus far I am unable to combine the results for Questions 1, 2, and 3 into 1 table.
The code I have written is:
tableq1<-percent(table(Q1val)/length(na.omit(Q1val)))
tableq1
The current output is:
What do I need to do in order to achieve this?
Ultimately, I want to have this table as a pdf or png, with gridlines that make it look clean and professional.
Per request:
dput(Q1val)
c(4, 5, 5, 5, 4, 5, 5, 5, 5, 5, 5, 4, 5, 5, 5, 4, 4, 3, 4, 5,
4, 5, 1, 5, 5, 5, 4, 5, 4, 3, 5, 5, 5, 5, 5, 5, 5, 4, 5, 5, 5,
5, 5, 3, 5, 5, 4, 4, 4, 4, 5, 2, 5, 4, NA, 5, 5, 5, 3, 5, 5,
4, 5, 5, 4, 5, 5, 4, 4, 5, 4, 5, 5, 4, 5, 5, 5, 5, 5, 4, 5, 4,
4, 3, 5, 4, 4, 5, 5, 5, 4, 5, 4, 5, 4, 4, 3, 5, 5, 5, 5, 4, 3,
4, 5, 4, 4, 5, 5, 4, 4, 5, 5, 4, 5, 4, 4, 4, 5, 4, 4, 5, 5, 5,
5, 5, 4, 4, 5, 4, 5, 5, 5, 2, 4, 2, 5, 5, 3, 4, 3, 4, 5, 5, 4,
4, 3, 5, 5, 4, 5, 4, 3, 5, 3, 4, 4, 5, 4, 5, 5, 5, 5, 5, 5, 5,
5, 5, 4, 5, 5, 3, 3, 4, 5, 4, 5, 5 # , ...
)
In an improvement over my comments, I've chosen to render the result with the kableExtra package.
Solution
First load the appropriate packages:
library(dplyr)
library(formattable)
library(kableExtra)
Once you've generated the Q*val vectors in your workspace...
# ...
# Code to generate 'Q1val', 'Q2val', ...
# ...
...then run this workflow to consolidate them into a single contingency table:
# Identify all the 'Q*val' vectors.
q_names <- ls(pattern = "Q\\d+val")
# Assemble a single table.
q_all <- q_names %>%
# Capture those vectors in a (named) list.
mget(inherits = TRUE) %>%
# Turn each vector into a contingency table of percentages.
sapply(
FUN = function(x) {
percent(table(x) / length(na.omit(x)))
},
simplify = FALSE
) %>%
# Stack those contingency tables into a single table.
do.call(what = bind_rows) %>%
# Convert to 'data.frame' to preserve row names.
as.data.frame() %>%
# Add row names of "Q1", "Q2", ...; as extracted from the names "Q1val", "Q2val", ...
`rownames<-`(value = gsub(
x = q_names,
pattern = "^(Q\\d+)(val)$",
replacement = "\\1"
))
You can then prettify the table and export it as an image.
# Prettify the table.
q_pretty <- q_all %>%
# Convert into 'kable' object.
kbl() %>%
# Border the rows and stripe the cells.
kable_styling(
bootstrap_options = c("bordered", "striped")
# ...
# Further styling as desired.
# ...
) # %>%
# ...
# Further 'kable' adjustments as desired.
# ...
# Save pretty table as PNG image.
save_kable(
x = q_pretty,
file = "pretty.png"
)
Note
You can easily save as a PDF by replacing "pretty.png" with "pretty.pdf".
Result
I had to improvise my own Q2val and Q3val, but the resulting pretty.png should look like this:

Creating a histogram with appropriate counts and labels in R

I have a dataset (dat), which I am hard-coding in here:
dat = c(5, 9, 5, 6, 5, 6, 8, 4, 6, 4, 6, 6, 4, 6, 4, 6, 5, 5, 6, 5, 6, 7, 4, 5, 4, 4, 6, 4, 4, 5, 7, 6, 3, 5, 5, 5, 5, 4, 6, 3, 6, 5, 4, 6, 5, 8, 4, 8, 5, 5, 4, 4, 6, 6, 4, 6, 4, 7, 4, 1, 4, 6, 3, 6, 3, 4, 6, 6, 3, 6, 6, 2, 5, 5, 4, 7, 6)
table(dat)
By doing the table function above on the data, I see that there should be a count of 1 for values of 1, and count of 1 for values of 2. However, when I plot the data using hist, I get a count of 2.
hist(dat, col="lightgreen", labels = TRUE, xlim=c(0,10), ylim=c(0,27))
This is the first problem. The other problem is that I am trying to plot the x label value for the corresponding bin (where there should be 11 bins, labeled 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10). Even though I have no 0 values or 10 values, I would like to illustrate that they had a count of 0, and have their bins - like the rest- labeled. How can I accomplish that?
Thanks.
am = hist(dat, col="lightgreen", labels = TRUE,
breaks=seq(min(dat)-2,max(dat)),
axes=F)
axis(2)
axis(1,at=am$mids,seq(min(dat)-1,max(dat)))
Did you mean like this:
hist(dat, col="lightgreen", labels = TRUE,
xlim=c(0,10), ylim=c(0,27), breaks = 0:10, at=0:10)

Resources