根据 nchar 更改列值
Change a column value based on nchar
我有以下问题:考虑一个具有列标题和副标题的等长数据框。 title 是相当干净的数据,而 subtitle 是相当混乱的(错误的值,NAs,...)但是,当正确填写 subtitle 时,它包含的信息比我的 title 变量多得多。
如果某个字幕观察的 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) , ]
应对 NA 的替代方法
#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) , ]
现在,当我 运行 两个示例之一时:我收到以下错误:
Error in
baseTable_sentiment$subtitle[(nchar(baseTable_sentiment$subtitle, :
incorrect number of dimensions
但是:当我检查所有使用的尺寸时
> > > 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
我该如何解决这个问题,或者你们有其他方法来执行此操作吗?
下面link包含一个data example
提前致谢
奥利维尔
我发现了错误:我的标题的维度不再对应于分配后条件创建的新维度。
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) ]
尺寸现已匹配,代码运行完美
我有以下问题:考虑一个具有列标题和副标题的等长数据框。 title 是相当干净的数据,而 subtitle 是相当混乱的(错误的值,NAs,...)但是,当正确填写 subtitle 时,它包含的信息比我的 title 变量多得多。
如果某个字幕观察的 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) , ]
应对 NA 的替代方法
#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) , ]
现在,当我 运行 两个示例之一时:我收到以下错误:
Error in baseTable_sentiment$subtitle[(nchar(baseTable_sentiment$subtitle, :
incorrect number of dimensions
但是:当我检查所有使用的尺寸时
> > > 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
我该如何解决这个问题,或者你们有其他方法来执行此操作吗?
下面link包含一个data example
提前致谢
奥利维尔
我发现了错误:我的标题的维度不再对应于分配后条件创建的新维度。
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) ]
尺寸现已匹配,代码运行完美