如何在R的心理软件包中使用带有三个感兴趣变量的summary.df函数?

时间:2019-06-05 21:03:08

标签: r mean psych

我想使用心理软件包中的desribeBy函数找到我数据集中趋势的度量。它似乎不起作用,并说:“参数不是数字或逻辑:返回NA”。在某些背景下,我正在研究Facebook使用情况(以“用户”或“非用户”衡量)与政治利益(以1-7范围衡量)之间的关系。

setwd("~/Desktop")

df <- read.csv("2016 ANES.csv")

# DV: V162174a (POST: Days in past week discussed politics)  
# IV: V162370 (POST: Facebook account used recently)
# Control: V161270 (PRE: Highest level of Education)

# Cleaning up my Dependent Variable; Political Involvement

table(df$V162174a) 

# I ignored -1, -6, -7, -9 and -8 (non-responses) when recoding my variable; 'Political interest' 

df$Political_interest[df$V162174a=="0. Zero days"] <- 0
df$Political_interest[df$V162174a=="1. One day"] <- 1
df$Political_interest[df$V162174a=="2. Two days"] <- 2
df$Political_interest[df$V162174a=="3. Three days"] <- 3
df$Political_interest[df$V162174a=="4. Four days"] <- 4
df$Political_interest[df$V162174a=="5. Five days"] <- 5
df$Political_interest[df$V162174a=="6. Six days"] <- 6
df$Political_interest[df$V162174a=="7. Seven days"] <- 7

table(df$Political_interest)

# Cleaning up my Independent variable; Facebook Usage

table(df$V162370)

df$Facebook_usage[df$V162370=="1. Yes have a Facebook account I have used in the past month"] <- "User"
df$Facebook_usage[df$V162370=="2. Have a Facebook account but have not used it in past month"] <- "Non_user"
df$Facebook_usage[df$V162370=="3. No, do not have a Facebook account"] <- "Non_user"

table(df$Facebook_usage)

# Comparing facebook usage and Political engagement

as.numeric(df$Facebook_usage)
na.omit(df$Facebook_usage)

table(df$Facebook_usage, df$Political_interest)

# Creating a grouped bar plot 

Political_interest <- table(df$Political_interest)
Political_interest

Facebook_usage <- table(df$User, df$Non_user)
User
Non_user

install.packages("plotly")
library(plotly)

Political_interest <- c("0", "1", "2", "3", "4", "5", "6", "7") 
User <- c(88, 195, 266, 196, 171, 181, 79, 514)
Non_user <- c(72, 169, 190, 156, 119, 123,  51, 308)
df <- data.frame(Political_interest, User, Non_user)

p <- plot_ly(df, x = ~Political_interest, y = ~User, type = 'bar', name = 'User') %>%
  add_trace(y = ~Non_user, name = 'Non User') %>%
  layout(yaxis = list(title = 'Respondents'), barmode = 'group')
p

# Looking at measures of central tendancy 

install.packages("psych")
library(psych)

describeBy(df$Political_interest, df$User, df$Non_user)

mean(df$Political_interest)

0 个答案:

没有答案