简体   繁体   English

用 0 和 1 替换 r 中的所有文本字符串

[英]replacing all text strings with 0 and 1 in r

I am working on a school project.我正在做一个学校项目。 My df contains a lot of character strings.我的 df 包含很多字符串。 Would I would like to do for my regression analysis is to replace any row that has a string with 1 to imply that yes something happens and any row that is blank with 0 to imply that there is nothing there.我想为我的回归分析做的是将任何具有字符串的行替换为 1 以暗示发生了某些事情,并将任何空白的行替换为 0 以暗示那里什么都没有。 For instance, a column is called indigenous and has many different language types for people that speak non common languages but for those that just speak english it is blank.例如,一列被称为土著,对于说非通用语言的人来说,它有许多不同的语言类型,但对于只说英语的人来说,它是空白的。 What i would want to do is that for any column that is filled make it a 1 and any column that is blank make it a zero.我想要做的是,对于任何已填充的列,将其设为 1,将任何空白列设为零。

Maybe you can try nzchar , eg,也许您可以尝试nzchar ,例如,

> +nzchar(s)
[1] 1 0 1

Data数据

s <- c("abc","","volvo")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM