R: Averaging columns and conditionally excluding NA data [duplicate] - r

This question already has answers here:
How can I get the average (mean) of selected columns
(3 answers)
Closed 5 years ago.
I'm trying to average across columns, however, because some of the columns are missing data, the average ends of being NA as well.
Is there a way to find the mean of a number of columns while excluding any NA data from the calculation?
The code I've used so far is:
### Calculate Bins ###
{pulse<-transmute(pulse, Question, Type, Student,Bin1=(Rt1+ Rt2 + Rt3+ Rt4)/4 , Bin2= (Rt5+Rt6+Rt7+Rt8)/4 , Bin3= (Rt9+Rt10+Rt11)/3)
}
However, I don't think this is the best way.My goal is to have three columns with the means of Rt1-Rt4, Rt5-Rt8 and Rt9-Rt11. i.e. something like this:
Question Type Student Bin1 Bin2 Bin3
1 Q SNR 789331 4.25 4.00 4.666667
2 Q2 SNR 789331 3.75 2.50 3.000000
3 Q8 SNR 789331 4.00 2.50 3.333333
4 Q10 SNR 789331 4.00 2.75 3.333333
5 Q12 SNR 789331 3.50 3.25 3.666667
Any help would be appreciated!
My data is attached below:
> dput(pulse)
structure(list(Question = c("Q", "Q2", "Q8", "Q10", "Q12", "Q",
"Q2", "Q8", "Q10", "Q12", "Q", "Q2", "Q8", "Q10", "Q12", "Q",
"Q2", "Q8", "Q10", "Q12", "Q", "Q2", "Q8", "Q10", "Q12", "Q",
"Q2", "Q8", "Q10", "Q12", "Q", "Q2", "Q8", "Q10", "Q12", "Q",
"Q2", "Q8", "Q10", "Q12", "Q", "Q2", "Q8", "Q10", "Q12", "Q",
"Q2", "Q8", "Q10", "Q12", "Q", "Q2", "Q8", "Q10", "Q12", "Q",
"Q2", "Q8", "Q10", "Q12", "Q", "Q2", "Q8", "Q10", "Q12", "Q",
"Q2", "Q8", "Q10", "Q12", "Q", "Q2", "Q8", "Q10", "Q12", "Q",
"Q2", "Q8", "Q10", "Q12", "Q", "Q2", "Q8", "Q10", "Q12", "Q",
"Q2", "Q8", "Q10", "Q12", "Q", "Q2", "Q8", "Q10", "Q12", "Q",
"Q2", "Q8", "Q10", "Q12", "Q", "Q2", "Q8", "Q10", "Q12", "Q",
"Q2", "Q8", "Q10", "Q12", "Q", "Q2", "Q8", "Q10", "Q12", "Q",
"Q2", "Q8", "Q10", "Q12", "Q", "Q2", "Q8", "Q10", "Q12", "Q",
"Q2", "Q8", "Q10", "Q12", "Q", "Q2", "Q8", "Q10", "Q12", "Q",
"Q2", "Q8", "Q10", "Q12", "Q", "Q2", "Q8", "Q10", "Q12", "Q",
"Q2", "Q8", "Q10", "Q12", "Q", "Q2", "Q8", "Q10", "Q12", "Q",
"Q2", "Q8", "Q10", "Q12", "Q", "Q2", "Q8", "Q10", "Q12", "Q",
"Q2", "Q8", "Q10", "Q12", "Q", "Q2", "Q8", "Q10", "Q12", "Q",
"Q2", "Q8", "Q10", "Q12", "Q", "Q2", "Q8", "Q10", "Q12", "Q",
"Q2", "Q8", "Q10", "Q12", "Q", "Q2", "Q8", "Q10", "Q12", "Q",
"Q2", "Q8", "Q10", "Q12", "Q", "Q2", "Q8", "Q10", "Q12"), Type = c("SNR",
"SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR",
"SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR",
"SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR",
"SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR",
"SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR",
"SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR",
"SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR",
"SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR",
"SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR",
"SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR",
"SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR",
"SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR",
"SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR", "SNR",
"SNR", "SNR", "FYS", "FYS", "FYS", "FYS", "FYS", "FYS", "FYS",
"FYS", "FYS", "FYS", "FYS", "FYS", "FYS", "FYS", "FYS", "FYS",
"FYS", "FYS", "FYS", "FYS", "FYS", "FYS", "FYS", "FYS", "FYS",
"FYS", "FYS", "FYS", "FYS", "FYS", "FYS", "FYS", "FYS", "FYS",
"FYS", "FYS", "FYS", "FYS", "FYS", "FYS", "FYS", "FYS", "FYS",
"FYS", "FYS", "FYS", "FYS", "FYS", "FYS", "FYS", "FYS", "FYS",
"FYS", "FYS", "FYS", "FYS", "FYS", "FYS", "FYS", "FYS", "FYS",
"FYS", "FYS", "FYS", "FYS", "FYS", "FYS", "FYS", "FYS", "FYS",
"FYS", "FYS", "FYS", "FYS", "FYS", "FYS", "FYS", "FYS", "FYS",
"FYS", "FYS", "FYS", "FYS", "FYS", "FYS"), Student = c("789331",
"789331", "789331", "789331", "789331", "805933", "805933", "805933",
"805933", "805933", "826523", "826523", "826523", "826523", "826523",
"832929", "832929", "832929", "832929", "832929", "838607", "838607",
"838607", "838607", "838607", "841903", "841903", "841903", "841903",
"841903", "843618", "843618", "843618", "843618", "843618", "852125",
"852125", "852125", "852125", "852125", "876406", "876406", "876406",
"876406", "876406", "879972", "879972", "879972", "879972", "879972",
"885650", "885650", "885650", "885650", "885650", "888712", "888712",
"888712", "888712", "888712", "903303", "903303", "903303", "903303",
"903303", "796882", "796882", "796882", "796882", "796882", "827911",
"827911", "827911", "827911", "827911", "830271", "830271", "830271",
"830271", "830271", "831487", "831487", "831487", "831487", "831487",
"834598", "834598", "834598", "834598", "834598", "836364", "836364",
"836364", "836364", "836364", "839802", "839802", "839802", "839802",
"839802", "855524", "855524", "855524", "855524", "855524", "873527",
"873527", "873527", "873527", "873527", "885409", "885409", "885409",
"885409", "885409", "894218", "894218", "894218", "894218", "894218",
"928026", "928026", "928026", "928026", "928026", "932196", "932196",
"932196", "932196", "932196", "955389", "955389", "955389", "955389",
"955389", "956952", "956952", "956952", "956952", "956952", "957206",
"957206", "957206", "957206", "957206", "957759", "957759", "957759",
"957759", "957759", "959200", "959200", "959200", "959200", "959200",
"962490", "962490", "962490", "962490", "962490", "968728", "968728",
"968728", "968728", "968728", "969005", "969005", "969005", "969005",
"969005", "971179", "971179", "971179", "971179", "971179", "976863",
"976863", "976863", "976863", "976863", "981621", "981621", "981621",
"981621", "981621", "952797", "952797", "952797", "952797", "952797",
"965873", "965873", "965873", "965873", "965873", "967416", "967416",
"967416", "967416", "967416", "975424", "975424", "975424", "975424",
"975424"), Rt1 = c(4, 3, 4, 4, 3, 5, 4, 5, 5, 5, 4, 4, 4, 5,
5, 4, 4, 4, 4, 3, 5, 5, 5, 5, 5, 2, 3, 4, 3, 4, 4, 5, 5, 4, 4,
3, 3, 3, 4, 3, 3, 3, 4, 4, 4, 3, 4, 5, 4, 3, 4, 4, 4, 3, 5, 4,
4, 4, 5, 5, 3, 4, 4, 4, 3, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 4, 5, 3, 4, 4, 4, 3, 3, 5, 4, 4, 2, 2, 3, 4, NA, NA,
NA, NA, NA, 3, 4, 4, 4, 3, NA, NA, NA, NA, NA, 5, 4, 5, 4, 4,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 4, 4, 3, 3, 4, 1, 3,
4, 5, 4, 4, 4, 5, 4, 4, NA, NA, NA, NA, NA), Rt2 = c(4, 4, 4,
4, 3, 4, 4, 4,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A, NA, NA,
NA, NA, 3, 5, 5, 5, 5, 4, 4, 4, 5, 4, 5, 5, 4, 4, 3, 4, 3, 3,
3, 3, 4, 4, 4, 4, 4, 4, 4, 2, 4, 4, 3, 3, 3, 3, 3, 5, 5, 4, 4,
5, 5, 5, 4, 5, 5, 4, 3, 3, 4, 4, 5, 5, 5, 3, 3, 5, 4, 4, 4, 4,
3, 2, 2, 2, 2, 5, 5, 5, 5, 5, NA, NA, NA, NA, NA), Rt9 = c(4,
3, 3, 3, 3, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 4, 3, 4, 4, 4, 4, 4, 4, 4, 5, 4, 3, 3, 4, 4, NA, NA,
NA, NA, NA, 3, 3, 3, 2, 4, 4, 4, 4, 4, 4, 5, 4, 4, 3, 3, 5, 4,
4, 4, 4, 3, 4, 4, 4, 4, 3, 1, 1, 1, 5, NA, NA, NA, NA, NA, 5,
5, 5, 5, 5, 5, 5, 5, 4, 5, NA, NA, NA, NA, NA, 3, 4, 3, 3, 4,
3, 3, 3, 2, 3, 5, 5, 5, 5, 5, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 4, 5, 5, 4, 4, NA, NA, NA, NA, NA, 5, 4, 3, 4, 4, 4, 3, 3,
3, 2, NA, NA, NA, NA, NA, 1, 1, 1, 1, 1, 2, 3, 4, 4, 2, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 4, 1, 1, 1, 1, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA), Rt10 = c(5, 3, 3, 3, 4, NA, NA, NA, NA,
NA, 5, 4, 4, 4, 4, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 5, 4, 4, 3, 4, 4, 3, 3, 3, 4, 4, 3, 2, 3, 4,
4, 4, 4, 4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5, 5, 5, 5, 4, 4, 4, 4, 4, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA)), .Names = c("Question", "Type",
"Student", "Rt1", "Rt2", "Rt3", "Rt4", "Rt5", "Rt6", "Rt7", "Rt8",
"Rt9", "Rt10", "Rt11"), row.names = c(NA, -205L), class = c("tbl_df",
"tbl", "data.frame"))

To generate mean of rows:
dataframe <- pulse[(number_of_rows_you_are_interested_in),]
rowMeans(dataframe, na.rm = TRUE)

Rt1[!is.na(Rt1)]
The above code returns the reduced dataframe by excluding all NA entries in Rt1
You may use this expression across your columns

I've found complete.cases() to be particularly useful to only give you rows that have no NAs
pulse <- pulse[complete.cases(pulse), ]
and then you should be able to calculate over this dataframe
also, instead of having to manually calculate the average, follow this link's example (which is pretty similar to your question to begin with)

Related

Removing a row based on a condition

my_df <- tibble(
b1 = c(2, 1, 1, 2, 2, 2, 1, 1, 2),
b2 = c(NA, 4, 6, 2, 6, 6, 1, 1, 7),
b3 = c(5, 9, 8, NA, 2, 3, 9, 5, NA),
b4 = c(NA, 6, NA, 10, 12, 8, 3, 6, 2),
b5 = c(2, 12, 1, 7, 8, 5, 5, 6, NA),
b6 = c(9, 2, 4, 6, 7, 6, 6, 7, 9),
b7 = c(1, 3, 7, 7, 4, 2, 2, 9, 5),
b8 = c(NA, 8, 4, 5, 1, 4, 1, 3, 6),
b9 = c(4, 5, NA, 9, 5, 1, 1, 2, NA),
b10 = c(14, 3, NA, 2, 2, 2, 3, NA, 5))
I have a df like this, and would like to tell R to remove all '3' or 'NA' in b10 if b1 = 1. I have tried this with this, but it seems to keep the '3' and 'NA' instead of removing them;
new_df <- my_df %>% filter(is.na(b10) | b10 == 3 | b1==1 & b10 ==NA)
This should do the trick:
library(dplyr)
my_df %>%
filter(!(b1 == 1 & (is.na(b10) | b10 == 3)))
Edit: assuming you want to remove the rows where the conditions meet

Filter data frame to get only rows that have a value in column and another value in any column after first value, R

I am looking for a methodology to filter the following data frame so that I end up with only the rows that have a 1 in some column and a 2 in any other column after the column containing the 1. I am thankful for any help!
data_rel1 <- structure(list(job1category = c(NA, 1, 2, 2, 1, 1, 2, 1, 1, 1,
1, 1, 1, 1, NA, 1, 1, 4, 1, 1, NA, NA, 1, 1, 1, 1, 1, 1, 2, 1,
1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 6, 1, 1, 1, 1, NA, 7, NA, 5, 1,
5, NA, 2, 5, 1, NA, 1, 5, 1, 1, 1, 1, 5, 1, 5, 4, 1, 4, 5, 4,
NA, 5, NA, 5, 4, 3, 6, 1, 4, 4, 5, 4, 1, NA, 1, NA, 1, NA, 1,
1, 1, 1, 1, 4, 1, 1, 1, NA, 1, NA), job2category = c(3, 2, 1,
2, 3, 1, 2, 2, 1, 1, 1, NA, 2, 1, NA, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 5, 3, 3, 1, 1, 2, 4, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1,
1, 1, 7, 7, 1, 1, 1, NA, 4, 1, 1, NA, 2, 1, 1, 1, 1, NA, 5, NA,
4, 5, 4, NA, 5, 2, 4, 4, 2, 7, 5, NA, 5, 2, NA, 4, NA, 1, 5,
NA, 1, NA, 1, 1, 1, 1, 5, 2, NA, 4, 1, 1, 1, NA, 1, NA), job3category = c(3,
2, 1, 2, 3, 1, 2, 2, 1, 1, 1, NA, 2, 1, NA, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 5, 3, 3, 1, 1, 2, 4, 1, 1, 1, 1, 1, 1, 1, 1, 2,
2, 1, 1, 1, 7, 7, 1, 1, 1, NA, 4, 1, 1, NA, 2, 1, 1, 1, 1, NA,
5, NA, 4, 5, 4, NA, 5, 2, 4, 4, 2, 7, 5, NA, 5, 2, NA, 4, NA,
1, 5, NA, 1, NA, 1, 1, 1, 1, 5, 2, NA, 4, 1, 1, 1, NA, 1, NA),
job4category = c(3, 2, 1, 2, 3, 1, 2, 2, 1, 1, 1, NA, 2,
1, NA, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 3, 3, 1, 1, 2,
4, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 7, 7, 1, 1, 1,
NA, 4, 1, 1, NA, 2, 1, 1, 1, 1, NA, 5, NA, 4, 5, 4, NA, 5,
2, 4, 4, 2, 7, 5, NA, 5, 2, NA, 4, NA, 1, 5, NA, 1, NA, 1,
1, 1, 1, 5, 2, NA, 4, 1, 1, 1, NA, 1, NA)), row.names = c(NA,
-100L), class = c("tbl_df", "tbl", "data.frame"))
You can try this with an apply -
data_rel1[apply(data_rel1, 1, function(x) {
inds <- which(x == 1)
length(inds) && any(which(x == 2) > min(inds))
}), ]
# job1category job2category job3category job4category
# <dbl> <dbl> <dbl> <dbl>
#1 1 2 2 2
#2 1 2 2 2
#3 1 2 2 2
#4 1 2 2 2
#5 1 2 2 2
#6 1 2 2 2
#7 1 2 2 2
#8 1 2 2 2

Stargazer descriptive table does not calculate data in R

I am trying to create a descriptive table (to export to LaTeX) with Stargazer. For some reason, in some datasets, like the one below, it only yields the first line (the column titles of the table).
I am not sure why this happens. It seems there is something wrong with my data. I saw a similar problem here, but the desired table was not of summary statistics. In my case, if I do the solution presente there (turn the dataset into a matrix), the table yielded is of the entire dataset, not descriptive stats.
Below is a code for a sample data and what I tried to produce the tables.
df <- structure(list(country = structure(c(26L, 5L, 26L, 25L, 25L,
3L, 27L, 27L, 27L, 6L, 27L, 26L, 6L, 10L, 1L, 5L, 27L, 20L, 27L,
10L, 26L, 1L, 2L, 15L, 10L, 1L, 26L, 17L, 9L, 7L, 11L, 1L, 26L,
20L, 27L, 6L, 1L, 25L, 9L, 2L, 22L, 15L, 23L, 27L, 9L, 27L, 8L,
27L, 19L, 24L, 20L, 19L, 22L, 26L, 26L, 10L, 10L, 16L, 20L, 25L,
2L, 26L, 1L, 27L, 11L, 1L, 2L, 8L, 25L, 27L, 26L, 9L, 10L, 4L,
9L, 27L, 7L, 19L, 27L, 26L, 6L, 17L, 11L, 16L, 16L, 26L, 27L,
26L, 27L, 14L, 23L, 27L, 23L, 10L, 9L, 23L, 22L, 26L, 26L, 27L
), .Label = c("Austria", "Belgium", "Bulgaria", "Cyprus", "Czech Republic",
"Denmark", "Estonia", "Finland", "France", "Germany", "Greece",
"Hungary", "Ireland", "Italy", "Latvia", "Lithuania", "Luxembourg",
"Malta", "Netherlands", "Poland", "Portugal", "Romania", "Slovakia",
"Slovenia", "Spain", "Sweden", "United Kingdom"), class = "factor"),
ptyname_eng = structure(c("Centre Party", "Green Party",
"Moderate Party", "Europe of the Peoples-The Greens", "Europe of the Peoples-The Greens",
"Order, Lawfulness, and Justice (RZS)", "Labour", "Green Party",
"Labour", "Liberal Party", "Independent", "Liberal People's Party",
"Danish People's Party", "Free Democratic Party", "Austrian People's Party",
"Christian and Democratic Union-Czechoslovak People's Party",
"Green Party", "Coalition Agreement for the Future - CenterLeft",
"UK Independence Party (UKIP)", "Free Democratic Party",
"Centre Party", "The Greens", "Ecolo", "Libertas", "Free Voters",
"The Greens", "Centre Party", "Christian Social People's Party",
"Europe Écologie", "People's Union of Estonia", "Action",
"Communist Party of Austria", "Centre Party", "Coalition Agreement for the Future - CenterLeft",
"Jury Team", "Social Democrats", "Austrian People's Party",
"Union, Progress, and Democracy", "The Left Front (Left Party + French Communist Party)",
"Ecolo", "Greater Romania Party", "Harmony Centre", "Green Party",
"Green Party", "Socialist Party", "No2EU: Yes to Democracy",
"Social Democratic Party of Finland", "Conservatives", "Libertas",
"For Real", "Civic Platform", "Christian Union-Reformed Political Party",
"Democratic Liberal Party", "Sweden Democrats", "Green Party",
"Free Democratic Party", "Christian Democratic Union/Christian Social Union",
"Civic Democratic Party", "Civic Platform", "Union, Progress, and Democracy",
"Christian Democratic and Flemish Party", "Pirate Party",
"The Greens", "Socialist Labour Party SLP", "New Democracy",
"The Greens", "Christian Democratic and Flemish Party", "Left Alliance",
"Union, Progress, and Democracy", "British National Party (BNP)",
"Left Party", "The Left Front (Left Party + French Communist Party)",
"Christian Social Democrats", "Progressive Party of Working People",
"Socialist Party", "UK Independence Party (UKIP)", "Social Democratic Party",
"Democrats 66", "Mebyon Kernow", "June List", "Socialist People's Party",
"Christian Social People's Party", "New Democracy", "Frontas Party",
"Homeland Union - Lithuanian Christian Democrats", "Liberal People's Party",
"No2EU: Yes to Democracy", "Centre Party", "Pro Democracy: Libertas.eu",
"Anticapitalist List", "Conservative Democrats of Slovakia",
"Christian Party - Christian People's Alliance", "Slovak Democratic and Christian Union",
"Free Democratic Party", "Europe Écologie", "Direction - Social Democracy",
"Democratic Union of Hungarians in Romania", "Centre Party",
"Liberal People's Party", "Conservatives"), label = "Party name (in English)", format.stata = "%75s"),
votes = c(5.47, 2.06, 18.83, 2.49, 2.49, 4.67, 15.7, 8.6,
15.7, 20.23, NA, 13.58, 15.28, 11, 29.98, 7.64, 8.6, 2.44,
16.5, 11, 5.47, 9.93, 8.64, 4.3, 1.7, 9.93, 5.47, 31.32,
16.28, 2.2, 0.76, 0.66, 5.47, 2.44, 0.5, 21.49, 29.98, 2.85,
6.05, 8.64, 8.65, 19.57, 2.11, 8.6, 16.48, 1.01, 17.5, 27.7,
0.32, 9.76, 44.43, 6.82, 29.71, 3.27, 11.02, 11, 30.7, 1.35,
44.43, 2.85, 14.43, 7.13, 9.93, 1.1, 32.3, 9.93, 14.43, 5.9,
2.85, 6.2, 5.66, 6.05, 7.2, 34.9, 16.48, 16.5, 8.7, 11.32,
0.1, 3.55, 15.86, 31.32, 32.3, 2.43, 26.86, 13.58, 1.01,
5.47, 0.5, 3.39, 2.1, 1.6, 16.98, 11, 16.28, 32.01, 8.92,
5.47, 13.58, 27.7), seats = c(1, 0, 4, 1, 1, 0, 13, 2, 13,
3, NA, 3, 2, 12, 6, 2, 2, 0, 13, 12, 1, 2, 2, 0, 0, 2, 1,
3, 14, 0, 0, 0, 1, 0, 0, 4, 6, 1, 4, 2, 3, 2, 0, 2, 14, 0,
2, 25, 0, 1, 25, 2, 10, 0, 2, 12, 34, 0, 25, 1, 3, 1, 2,
0, 8, 2, 3, 0, 1, 2, 1, 4, 8, 2, 14, 13, 1, 3, 0, 0, 2, 3,
8, 0, 4, 3, 0, 1, 0, 0, 0, 0, 2, 12, 14, 5, 3, 1, 3, 25),
lsq = c(5.65121280548163, 11.0409569967897, 5.65121280548163,
2.09070598337411, 2.09070598337411, 18.4291883786975, 7.64222053188085,
7.64222053188085, 7.64222053188085, 8.49009926343377, NA,
5.65121280548163, 8.49009926343377, 4.03227351136326, 5.21894849144935,
11.0409569967897, 7.64222053188085, 4.87412556260095, 7.64222053188085,
4.03227351136326, 5.65121280548163, 5.21894849144935, 4.16813185258047,
9.99651439252703, 4.03227351136326, 5.21894849144935, 5.65121280548163,
14.683991850538, 10.16545, 10.829844309951, 3.4795321239576,
5.21894849144935, 5.65121280548163, 4.87412556260095, 7.64222053188085,
8.49009926343377, 5.21894849144935, 2.09070598337411, 10.1654489717407,
4.16813185258047, 2.66691671130863, 9.99651439252703, 7.88621704182489,
7.64222053188085, 10.1654489717407, 7.64222053188085, 6.48086623985829,
7.64222053188085, 3.63340749159794, 10.4201368043039, 4.87412556260095,
3.63340749159794, 2.66691671130863, 5.65121280548163, 5.65121280548163,
4.03227351136326, 4.03227351136326, 8.7655582315738, 4.87412556260095,
2.09070598337411, 4.16813185258047, 5.65121280548163, 5.21894849144935,
7.64222053188085, 3.4795321239576, 5.21894849144935, 4.16813185258047,
6.48086623985829, 2.09070598337411, 7.64222053188085, 5.65121280548163,
10.1654489717407, 4.03227351136326, 6.81012399952372, 10.1654489717407,
7.64222053188085, 10.829844309951, 3.63340749159794, 7.64222053188085,
5.65121280548163, 8.49009926343377, 14.683991850538, 3.4795321239576,
8.7655582315738, 8.7655582315738, 5.65121280548163, 7.64222053188085,
5.65121280548163, 7.64222053188085, 6.15555836341693, 7.88621704182489,
7.64222053188085, 7.88621704182489, 4.03227351136326, 10.16545,
7.88621704182489, 2.66691671130863, 5.65121280548163, 5.65121280548163,
7.64222053188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A, NA, 5,
2, 7, 3, 8, 4, 0, 4, 0, 7, 7, NA, 0, 0, 3, 8, 5, NA, 7, 5,
10, 8, 4, 5, 8, 5, 8, NA, 5, 2, 0, 10, 6, 3, 3, 2, 4, 6,
NA, 0, 8, 4, 0, 6, NA, 4, 3, 5, NA, 7, 6, NA, 9, 8, 0, 6,
5, 0, 8, 6, 8, 5, 2, NA, 6, 5, 7, 7), v020_05 = c(1, 1, 1,
1, 3, 1, 1, 1, 2, NA, 1, 2, 4, 2, 4, 3, 1, NA, 3, 2, 1, 1,
4, NA, 2, 1, 2, 3, 1, 1, NA, NA, 2, 1, 1, 1, 2, 4, 1, 2,
1, 1, 1, 1, 1, 1, 1, 1, 3, NA, 5, 4, 1, 4, 2, 2, 1, 4, 2,
NA, 3, 1, 1, 1, 1, 1, 2, 1, 3, 4, NA, 1, 4, 3, 1, 3, NA,
1, 1, 2, NA, 4, 1, NA, 4, 2, 1, 1, 3, 1, 4, 2, 2, 4, 1, NA,
1, 2, 1, 5), v020_02 = c(1, 3, 1, 5, 4, 1, 3, 5, 4, NA, 3,
2, 3, 2, 1, 3, 5, NA, 1, 1, 1, 5, 4, NA, 1, 2, 2, 1, 5, 2,
NA, NA, 3, 3, 1, 5, 3, 2, 5, 4, 1, 2, 2, 5, 5, 5, 3, 3, 1,
NA, 1, 4, 1, 2, 4, 1, 1, 3, 1, NA, 3, 4, 4, 4, 3, 4, 5, 4,
2, 3, NA, 5, 1, 2, 2, 1, NA, 2, 3, 2, NA, 1, 2, NA, 2, 2,
5, 2, 1, 4, 2, 4, 2, 2, NA, NA, 2, 3, 1, 1)), row.names = c(NA,
-100L), class = c("tbl_df", "tbl", "data.frame"))
stargazer (df)
stargazer(as.matrix(df)
df <- as.matrix(df)
stargazer(df)
Convert the data to dataframe.
stargazer::stargazer(data.frame(df))
With type = 'text'
stargazer::stargazer(data.frame(df), type = 'text')
===========================================================
Statistic N Mean St. Dev. Min Pctl(25) Pctl(75) Max
-----------------------------------------------------------
votes 99 11.784 10.443 0.100 3.330 16.380 44.430
seats 99 4.515 6.649 0.000 0.500 4.000 34.000
lsq 99 6.627 2.903 2.091 4.521 7.886 18.429
v020_03 88 3.852 1.352 1.000 3.000 5.000 5.000
v020_04 87 3.172 1.340 1.000 2.000 4.000 5.000
v020_08 87 2.828 1.278 1.000 2.000 4.000 5.000
v018_1 86 4.767 2.660 0.000 3.000 7.000 10.000
v020_05 88 1.966 1.198 1.000 1.000 3.000 5.000
v020_02 87 2.667 1.403 1.000 1.000 4.000 5.000
-----------------------------------------------------------
We can also convert to tibble
stargazer::stargazer(as_tibble(df))

W-NOMINATE method in R: Error Data contains values other than x or y or z

I realize that I may ask this question to a void because wnominate package is not of a wide use, but...
I have data on evaluation of state policies. I would like to know the potential choice polarization using W-NOMINATE Method. I have prepared my data according to the wnominate package vignette (p. 9, 11). But when I try to apply the method to my data, I receive a very strange error:
Error in wnominate(rc_samp, polarity = c(2, 7)) :
Data contains values other than 1 or 6 or 9.
And I do not understand what I am asked about. Can anybody clarify, what I am doing wrong? It will be a miracle if someone can help me with this package.
Data and code:
respNames <- samp$id
codeData <- matrix(samp$code, length(samp$code), 1)
colnames(codeData) <- "code"
samp <- samp[, -c(1,2)]
rc_samp <- rollcall(samp, yea = c(5,6,7), nay = c(1,2,3), missing = 4,
notInLegis = 88, legis.names = respNames, legis.data = codeData, desc = "Ideological polarization")
samp_result <- wnominate(rc_samp, polarity = c(7,7))
structure(list(id = structure(1:100, .Label = c("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", "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", "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", "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", "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", "161", "162", "163", "164", "165",
"166", "167", "168", "169", "170", "171", "172", "173", "174",
"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", "201",
"202", "203", "204", "205", "206", "207", "208", "209", "210",
"211", "212", "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", "266", "267", "268", "269", "270", "271", "272", "273",
"274", "275", "276", "277", "278", "279", "280", "281", "282",
"283", "284", "285", "286", "287", "288", "289", "290", "291",
"292", "293", "294", "295", "296", "297", "298", "299", "300",
"301", "302", "303", "304", "305", "306", "307", "308", "309",
"310", "311", "312", "313", "314", "315", "316", "317", "318",
"319", "320", "321", "322", "323", "324", "325", "326", "327",
"328", "329", "330", "331", "332", "333", "334", "335", "336",
"337", "338", "339", "340", "341", "342", "343", "344", "345",
"346", "347", "348"), class = "factor"), code = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2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 2, 6, 2, 3,
4, 6, 2, 1, 2, 2, 7, 4, 4, 2, 2, 88, 1, 6, 1, 6, 2, 1, 7,
6, 1, 3, 2, 1, 2, 1, 5, 4, 6, 1, 4, 4, 2, 4, 4, 1, 2, 3,
7, 5, 2, 1, 2, 4, 88, 88, 1, 3, 5, 88, 5, 4, 3, 5, 5, 2,
3, 4, 88, 2, 4, 88, 1, 4, 3, 3, 3, 1, 2, 3, 7, 3, 4, 4, 1,
1, 1, 3, 1, 4, 4, 2, 4, 2, 4, 5, 1, 3), ideol_privatefreedom = c(5,
88, 7, 5, 7, 4, 3, 3, 5, 1, 6, 5, 4, 2, 5, 7, 6, 7, 1, 3,
3, 5, 5, 88, 7, 2, 6, 3, 5, 7, 2, 2, 7, 5, 5, 88, 6, 7, 3,
4, 1, 7, 4, 4, 3, 5, 5, 7, 2, 5, 1, 3, 6, 7, 4, 4, 3, 88,
7, 6, 3, 4, 5, 4, 5, 4, 4, 7, 7, 3, 88, 6, 6, 88, 7, 3, 5,
2, 4, 7, 4, 5, 1, 3, 5, 4, 5, 7, 5, 5, 5, 4, 5, 2, 5, 6,
5, 3, 88, 3), ideol_goveconomy_lib = c(5, 2, 4, 2, 6, 3,
1, 5, 2, 1, 5, 5, 4, 5, 2, 6, 2, 7, 1, 5, 4, 5, 3, 4, 2,
2, 6, 4, 5, 7, 2, 1, 2, 3, 5, 5, 88, 4, 3, 5, 5, 6, 1, 3,
2, 4, 5, 5, 5, 1, 5, 88, 5, 6, 4, 3, 2, 1, 2, 88, 5, 5, 1,
4, 6, 4, 1, 3, 4, 3, 6, 5, 4, 1, 7, 5, 1, 1, 2, 6, 3, 7,
1, 1, 3, 4, 1, 5, 4, 6, 6, 4, 6, 3, 3, 3, 2, 3, 1, 4)), row.names = c(NA,
-100L), class = c("tbl_df", "tbl", "data.frame"))
I contacted with the package creator and we came to the (though unilateral) conclusion that WNOMINATE method is applicable to the data with big number of columns.
Rollcall votes should be coded as either 1, 6, or 9.

Summarise grouping by varname

I'm analysing some data, and I've come a difficulty - I don't know how to summarise my whole dataset using the variable names as the "group".
A sample data:
structure(list(x4 = c(3, 4, 7, 4, 5, 1, 5, 2, 7, 1), x5 = c(2,
4, 4, 4, 5, 3, 6, 1, 7, 1), x6 = c(3, 5, 4, 7, 5, 4, 6, 4, 6,
2), x7 = c(4, 1, 6, 4, 6, 4, 6, 2, 7, 2), x9 = c(5, 5, 4, 5,
6, 3, 7, 5, 6, 1), x10 = c(3, 6, 5, 4, 6, 5, 6, 3, 6, 1), x11 = c(6,
7, 7, 7, 6, 7, 7, 5, 7, 4), x12 = c(6, 7, 6, 7, 6, 4, 6, 6, 7,
5), x14 = c(5, 7, 5, 6, 4, 6, 6, 5, 6, 4), x15 = c(4, 7, 7, 7,
6, 4, 6, 5, 6, 1), x16 = c(4, 7, 7, 7, 6, 5, 7, 3, 6, 4), x17 = c(4,
5, 5, 7, 6, 6, 7, 4, 6, 2), x18 = c(3, 4, 7, 7, 6, 5, 6, 4, 6,
2), x19 = c(5, 7, 5, 7, 6, 6, 6, 3, 6, 1), x22 = c(4, 4, 5, 7,
6, 7, 6, 5, 6, 2), x26 = c(6, 7, 5, 4, 6, 7, 7, 4, 6, 4), x29 = c(4,
7, 2, 7, 6, 4, 7, 3, 6, 1), x33 = c(3, 7, 7, 7, 6, 5, 6, 3, 6,
3), x34 = c(5, 5, 4, 7, 6, 7, 7, 5, 6, 2), x35 = c(4, 4, 7, 7,
5, 7, 6, 4, 6, 2), x36 = c(4, 7, 6, 7, 6, 5, 5, 4, 6, 2), x37 = c(3,
4, 7, 4, 5, 4, 6, 3, 5, 2), x49 = c(4, 7, 7, 7, 6, 5, 5, 6, 6,
3), x50 = c(4, 7, 6, 5, 5, 5, 6, 5, 7, 4)), row.names = c(NA,
-10L), class = "data.frame", .Names = c("x4", "x5", "x6", "x7",
"x9", "x10", "x11", "x12", "x14", "x15", "x16", "x17", "x18",
"x19", "x22", "x26", "x29", "x33", "x34", "x35", "x36", "x37",
"x49", "x50"))
I just want some statistics, like this:
summary <- dados_afc %>%
summarise_all(funs(mean, sd, mode, median))
But the result is a df with one observation and lots of variable. I wanted it to have 5 columns: varname, mean, sd, mode, median, but I'm not sure how to do it. Any tips?
Note: I am not aware of a built-in way to get mode from R. See here for some discussion:
Is there a built-in function for finding the mode?
# From the top answer there:
Mode <- function(x) {
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]
}
To treat each column as a group, you can use tidyr::gather to convert your "wide" data into long form, and then dplyr::group_by to create groups with their own summary calculations:
library(tidyverse)
summary <- dados_afc %>%
gather(group, value) %>%
group_by(group) %>%
summarise_all(funs(mean, sd, Mode, median))
> summary
# A tibble: 24 x 5
group mean sd Mode median
<chr> <dbl> <dbl> <dbl> <dbl>
1 x10 4.5 1.72 6 5
2 x11 6.3 1.06 7 7
3 x12 6 0.943 6 6
4 x14 5.4 0.966 6 5.5
5 x15 5.3 1.89 7 6
6 x16 5.6 1.51 7 6
7 x17 5.2 1.55 6 5.5
8 x18 5 1.70 6 5.5
9 x19 5.2 1.87 6 6
10 x22 5.2 1.55 6 5.5

Resources