简体   繁体   English

在 R 中,如何在列名中包含正斜杠?

[英]In R, how do i include a forward slash in the name of a column?

While creating my data.frame, I want one of my columns to include a '/' in the column name.在创建我的 data.frame 时,我希望我的列之一在列名中包含一个“/”。 When I try to put it in normally, when I check my object, the '/' is replaced by a period.当我尝试正常放入时,当我检查我的 object 时,'/' 被替换为句点。 Here is the code I am working with.这是我正在使用的代码。 My problem is with the 'S/NS' column.我的问题是“S/NS”列。

df_3 <- data.frame(age=numeric(7800),
                 duration=numeric(7800), 
                 value=numeric(7800),
                 'S/NS'=character(7800),
                 sex=character(7800),
                 stringsAsFactors=FALSE)

head(df_3) gives me the following output. head(df_3) 给了我以下 output。

age duration value S.NS sex
1   0        0     0         
2   0        0     0         
3   0        0     0         
4   0        0     0         
5   0        0     0         
6   0        0     0 

What I intended was for the column "N.NS" to appear as "N/NS" (smoking/non-smoking).我的意图是让“N.NS”列显示为“N/NS”(吸烟/不吸烟)。 I have tried fiddling around with the quotation marks, putting in a backslash escape before the forward slash, and some fruitless google searches.我曾尝试摆弄引号,在正斜杠之前放置一个反斜杠转义,以及一些徒劳的谷歌搜索。 Thanks in advance.提前致谢。

You can use check.names = F to disable name checking:您可以使用check.names = F禁用名称检查:

df_3 <- data.frame(age=numeric(7800),
                   duration=numeric(7800), 
                   value=numeric(7800),
                   'S/NS'=character(7800),
                   sex=character(7800),
                   stringsAsFactors=FALSE,check.names = F)

> colnames(df_3)
[1] "age"      "duration" "value"    "S/NS"     "sex"  

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

相关问题 如何将 rvest 与包含正斜杠的元素 ID 名称一起使用? - How do I use rvest with an element id name that contains a forward slash? 在 R 中,如何在变量后命名 Dataframe 列? - In R How Do I Name a Dataframe Column After a Variable? 如何命名r中的“行名称”列 - How do I name the “row names” column in r R:如何匹配正则表达式中的正斜杠? - R: How to match a forward-slash in a regular expression? 如何在R中将整个CSV文件的反斜杠替换为正斜杠? - How to replace backslash to forward slash for entire CSV file in R? 如何在R的循环中包含GLM - How do I include GLM in a loop in R 在 R 中,如何将时间序列 dataframe 向前移动 8 小时? - In R how do I shift a time series dataframe forward by 8 hours? 在R中:如何从字符串加上列名称再加上分类变量来创建数据框名称? - In R: How do I create a dataframe name from a string plus a column name plus categorical variable? 如何使用向量重命名R数据框中的列以指示OLD列名? - How do I rename a column in a R dataframe using a vector to indicate the OLD column name? 在 R 中,如何检查一个 dataframe 中的列名是否存在于另一个 dataframe 中,然后在该列中插入一个值? - In R, how do I check to see if a column name in one dataframe is present in another dataframe and then insert a value into that column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM