用双引号 ("") 保存字符列
Saving character columns with double quotes ("")
我想用双引号保存包含空格或空格的字符串列。
df <-
structure(list(type = c("menu", "item", "item", "item", "item",
"item"), menu.item = c("fileMenu", "fileMenu", "fileMenu", "fileMenu",
"fileMenu", "fileMenu"), operation.parent = c("topMenu", "command",
"separator", "command", "command", "command"), label = c("",
"Change working directory...", "", "Open script file...", "Save script...",
"Save script as..."), command.menu = c("", "Setwd", "", "loadLog",
"saveLog", "saveLogAs"), activation = c("", "", "", "", "", ""
), install. = c("", "", "", "", "", "")), .Names = c("type",
"menu.item", "operation.parent", "label", "command.menu", "activation",
"install."), row.names = c(NA, 6L), class = "data.frame"
)
df
type menu.item operation.parent label command.menu
1 menu fileMenu topMenu
2 item fileMenu command Change working directory... Setwd
3 item fileMenu separator
4 item fileMenu command Open script file... loadLog
5 item fileMenu command Save script... saveLog
6 item fileMenu command Save script as... saveLogAs
activation install.
1
2
3
4
5
6
df$label
[1] "" "Change working directory..."
[3] "" "Open script file..."
[5] "Save script..." "Save script as..."
您可以将 ""
替换为 '""'
。
如果你有这个:
df$label
#> [1] "" "Change working directory..."
#> [3] "" "Open script file..."
#> [5] "Save script..." "Save script as..."
如果您想用双引号将内容括起来,您可以这样做:
df$label <- paste0('"', df$label, '"')
df$label
#> [1] "\"\"" "\"Change working directory...\""
#> [3] "\"\"" "\"Open script file...\""
#> [5] "\"Save script...\"" "\"Save script as...\""
一次更新所有列
使用 base R(改编自@r2evans 的评论)
df[] <- lapply(df, function(x) paste0('"', x, '"'))
或者如果您更喜欢使用 dplyr
之类的东西,您可以这样做:
library(dplyr)
df <- mutate_all(df, ~ paste0('"', ., '"'))
我想用双引号保存包含空格或空格的字符串列。
df <-
structure(list(type = c("menu", "item", "item", "item", "item",
"item"), menu.item = c("fileMenu", "fileMenu", "fileMenu", "fileMenu",
"fileMenu", "fileMenu"), operation.parent = c("topMenu", "command",
"separator", "command", "command", "command"), label = c("",
"Change working directory...", "", "Open script file...", "Save script...",
"Save script as..."), command.menu = c("", "Setwd", "", "loadLog",
"saveLog", "saveLogAs"), activation = c("", "", "", "", "", ""
), install. = c("", "", "", "", "", "")), .Names = c("type",
"menu.item", "operation.parent", "label", "command.menu", "activation",
"install."), row.names = c(NA, 6L), class = "data.frame"
)
df
type menu.item operation.parent label command.menu
1 menu fileMenu topMenu
2 item fileMenu command Change working directory... Setwd
3 item fileMenu separator
4 item fileMenu command Open script file... loadLog
5 item fileMenu command Save script... saveLog
6 item fileMenu command Save script as... saveLogAs
activation install.
1
2
3
4
5
6
df$label
[1] "" "Change working directory..."
[3] "" "Open script file..."
[5] "Save script..." "Save script as..."
您可以将 ""
替换为 '""'
。
如果你有这个:
df$label
#> [1] "" "Change working directory..."
#> [3] "" "Open script file..."
#> [5] "Save script..." "Save script as..."
如果您想用双引号将内容括起来,您可以这样做:
df$label <- paste0('"', df$label, '"')
df$label
#> [1] "\"\"" "\"Change working directory...\""
#> [3] "\"\"" "\"Open script file...\""
#> [5] "\"Save script...\"" "\"Save script as...\""
一次更新所有列
使用 base R(改编自@r2evans 的评论)
df[] <- lapply(df, function(x) paste0('"', x, '"'))
或者如果您更喜欢使用 dplyr
之类的东西,您可以这样做:
library(dplyr)
df <- mutate_all(df, ~ paste0('"', ., '"'))