我有以下问题:考虑一个长度相等的数据帧,列标题和副标题。标题是相当干净的数据,字幕相当混乱(错误的值,NAs,......)但是,当正确填写字幕时,它包含的信息比我的标题变量多得多。
我想替换标题列中的值,其中某个字幕观察的nchar超过了标题观察的nchar。
目前我失败的代码看起来像这样:
#filter from real table
baseTable_sentiment <- filter(baseTable, theme_ecoFin == 1)
#with this code I try to do what I explained, while coping with the NA's in subtitle
baseTable_sentiment$title <- baseTable_sentiment$subtitle[nchar(baseTable_sentiment$subtitle , allowNA = TRUE , keepNA = TRUE) > nchar(baseTable_sentiment$title) , ]
应对国家行动方案的另一种方法
#filter from real table
baseTable_sentiment <- filter(baseTable, theme_ecoFin == 1)
#change NA to text value "na"
baseTable_sentiment$subtitle <- replace(baseTable_sentiment$subtitle,which(is.na(baseTable_sentiment$subtitle)),"na")
#same code as before
baseTable_sentiment$title <- baseTable_sentiment$subtitle[nchar(baseTable_sentiment$subtitle ) > nchar(baseTable_sentiment$title) , ]
现在,当我运行以下两个示例中的一个时:我收到以下错误:
错误 baseTable_sentiment $ subtitle [(nchar(baseTable_sentiment $ subtitle,:
) 维数不正确
然而:当我检查所有使用的尺寸时
> > > length(baseTable_sentiment$subtitle) [1] 170206
> > > length(baseTable_sentiment$title) [1] 170206
> > > length(nchar(baseTable_sentiment$subtitle , allowNA = TRUE ) > nchar(baseTable_sentiment$title)) [1] 170206
我该如何解决这个问题,或者你们有其他方法可以解决这个问题吗?
以下链接包含data example
提前谢谢
奥利弗
答案 0 :(得分:0)
我发现错误:我的标题的维度不再与分配后条件创建的新维度相对应。
baseTable_sentiment$title[nchar(baseTable_sentiment$subtitle , allowNA = TRUE , keepNA = FALSE) > nchar(baseTable_sentiment$title) ]<- baseTable_sentiment$subtitle[nchar(baseTable_sentiment$subtitle , allowNA = TRUE , keepNA = FALSE) > nchar(baseTable_sentiment$title) ]
尺寸现在匹配,代码运行完美