有一个包含对调查的回答的数据集。尝试将项目回答与主答案进行比较。所有列都是完整的文本值,我想创建一个将文本与主行匹配的标志,然后创建一个标志来对项目响应进行二分法。我看过整数的代码,但没有看过文本的代码。为了简单起见,我有大约25列,示例:
具备以下条件:
Chart 1 user Assistive device needed Assistance needed with 1 or more ADLs Oriented < 24 hours/day One hour or less if at least one of the disciplines None None
Chart 1 user Assistive device needed Assistance needed with 1 or more ADLs Oriented < 24 hours/day One hour or less if at least one of the disciplines None None
Chart 2 user Assistive device needed Independent Oriented No capable caregiver availability One hour or less if at least one of the disciplines None None
Chart 2 user Assistive device needed Independent Oriented < 24 hours/day One hour or less if at least one of the disciplines None None
Chart 2 user Assistive device needed Assistance needed with 1 or more ADLs Oriented < 24 hours/day One hour or less if at least one of the disciplines None None
Chart 1 expert Assistive device needed Assistance needed with 1 or more ADLs Oriented < 24 hours/day One hour or less if at least one of the disciplines None None
Chart 2 expert Assistive device needed Assistance needed with 1 or more ADLs Oriented < 24 hours/day One hour or less if at least one of the disciplines None None我想像这样输出一个数据框:
Chart 1 user 1 1 1 1 1 1 1
Chart 1 user 1 1 1 1 1 1 1
Chart 2 user 1 0 1 0 1 1 1
Chart 2 user 1 0 1 1 1 1 1
Chart 2 user 1 1 1 1 1 1 1当前代码:
amb_flg <- ifelse(c[,3] == "Assistive device needed",1,0)
adl_flg <- ifelse(c[,4] == "Assistance needed with 1 or more ADLs",1,0)
cog_flg <- ifelse(c[,5] == "Oriented",1,0)
cgv_flg <- ifelse(c[,6] == "< 24 hours/day",1,0)
tim_flg <- ifelse(c[,7] == "One hour or less if at least one of the disciplines",1,0)上面的问题是,我必须硬编码每个图表的答案(1对2)。为了提高效率,我想创建一个函数来生成一个标志,以确定该值是否与主值相对应。实验代码:
## Create agreement score
f.a <- function (a) {
y <- subset(c, c$user == 'expert')
z <- subset(c, c$user != 'expert')
df <- apply(z, 2, function(b){
for (i in y)
ifelse(i %in% z, 1, 0)
})
return(as.data.frame(df))
}
df <- f.a(c)发布于 2015-02-25 05:39:21
你的意思是这些方面的东西吗?如果你想在算法上识别主答案,那就复杂得多。
> df <- data.frame(Name = c("Jim", "John", "Amy", "Master"),
+ Item1 = c("Does agree", "Does not agree", "Does agree", "Does not agree"))
> df
Name Item1
1 Jim Does agree
2 John Does not agree
3 Amy Does agree
4 Master Does not agree
> df$Item1_flag <- ifelse(df$Item1 == "Does not agree", "expert", "not expert")
> df$Item1_count <- ifelse(df$Item1 == "Does not agree", "1", "0")
> df
Name Item1 Item1_flag Item1_count
1 Jim Does agree not expert 0
2 John Does not agree expert 1
3 Amy Does agree not expert 0
4 Master Does not agree expert 1https://stackoverflow.com/questions/28706322
复制相似问题