如何在 R DataFrame 中为列名添加前缀?
在本文中,我们将讨论如何在 R 编程语言中为 DataFrame 中的列名添加前缀。
使用中的数据集:First Second Third 1 a 7 2 ab 8 3 cv 9 4 dsd 10
方法一:使用 paste() 方法
为了修改列名,可以使用R中的paste函数。 paste() 方法可用于将字符串向量连接在一起以形成更大的字符串或句子。使用在 paste函数指定的分隔符连接字符串向量参数。更改必须保存到原始字符串,并且不会自行保留。字符串按照方法中的规范顺序连接在一起。如果我们在 R 列表对象上指定它,则该方法连续应用于每个字符串。
Syntax: paste( prefix1, prefix2.., colnames(df), sep=)
Parameter :
- colnames(df) – Column names of data frame
- prefix1.. – prefix string to be added to each column name
- sep – separator to use between the strings
例子:
R
# declaring a data frame
df <- data.frame(First = c(1,2,3,4),
Second = c("a","ab","cv","dsd"),
Third=c(7:10))
# print original data frame
print ("Original DataFrame : ")
print (df)
# printing original colnames of
# data frame
original_cols <- colnames(df)
print ("Original column names ")
print (original_cols)
# adding prefix using the paste
# function in R
colnames(df) <- paste("Column" ,original_cols,sep="-")
# print changed data frame
print ("Modified DataFrame : ")
print (df)
R
# declaring a data frame
df <- data.frame(First = c(1,2,3,4),
Second = c("a","ab","cv","dsd"),
Third=c(7:10))
# print original data frame
print ("Original DataFrame : ")
print (df)
# printing original colnames
# of data frame
original_cols <- colnames(df)
print ("Original column names ")
print (original_cols)
# adding prefix using the paste
# function in R
colnames(df) <- paste("Col" ,"No",original_cols,sep="_")
# print changed data frame
print ("Modified DataFrame : ")
print (df)
R
# declaring a data frame
df <- data.frame(First = c(1,2,3,4),
Second = c("a","ab","cv","dsd"),
Third=c(7:10))
# print original data frame
print ("Original DataFrame : ")
print (df)
# printing original colnames
# of data frame
original_cols <- colnames(df)
print ("Original column names ")
print (original_cols)
# adding prefix using the paste0
# function in R
colnames(df) <- paste0("Col" ,original_cols)
# print changed data frame
print ("Modified DataFrame : ")
print (df)
输出
[1] "Original DataFrame : "
First Second Third
1 1 a 7
2 2 ab 8
3 3 cv 9
4 4 dsd 10
[1] "Original column names "
[1] "First" "Second" "Third"
[1] "Modified DataFrame : "
Column-First Column-Second Column-Third
1 1 a 7
2 2 ab 8
3 3 cv 9
4 4 dsd 10
通过在此方法中指定列名之前的字符串,也可以在每个列名之前添加多个前缀。
例子:
电阻
# declaring a data frame
df <- data.frame(First = c(1,2,3,4),
Second = c("a","ab","cv","dsd"),
Third=c(7:10))
# print original data frame
print ("Original DataFrame : ")
print (df)
# printing original colnames
# of data frame
original_cols <- colnames(df)
print ("Original column names ")
print (original_cols)
# adding prefix using the paste
# function in R
colnames(df) <- paste("Col" ,"No",original_cols,sep="_")
# print changed data frame
print ("Modified DataFrame : ")
print (df)
输出
[1] "Original DataFrame : "
First Second Third
1 1 a 7
2 2 ab 8
3 3 cv 9
4 4 dsd 10
[1] "Original column names "
[1] "First" "Second" "Third"
[1] "Modified DataFrame : "
Col_No_First Col_No_Second Col_No_Third
1 1 a 7
2 2 ab 8
3 3 cv 9
4 4 dsd 10
方法 2:使用 paste0() 方法
paste0 方法在功能上与 paste 方法完全相似,但它不提供在字符串参数之间自定义分隔符的功能。它会自动折叠字符串参数并将它们连接起来以生成一个没有任何空格的更大的字符串。也可以使用这种方法添加多个前缀。如果我们在 R 列表对象上指定它,则该方法连续应用于每个字符串。
Syntax: paste0( prefix1, prefix2, colnames(df))
Parameter :
- colnames(df) – Column names of data frame
- prefix1.. – prefix string to be added to each column name. The strings are concatenated using sep=””.
例子:
电阻
# declaring a data frame
df <- data.frame(First = c(1,2,3,4),
Second = c("a","ab","cv","dsd"),
Third=c(7:10))
# print original data frame
print ("Original DataFrame : ")
print (df)
# printing original colnames
# of data frame
original_cols <- colnames(df)
print ("Original column names ")
print (original_cols)
# adding prefix using the paste0
# function in R
colnames(df) <- paste0("Col" ,original_cols)
# print changed data frame
print ("Modified DataFrame : ")
print (df)
输出
[1] "Original DataFrame : "
First Second Third
1 1 a 7
2 2 ab 8
3 3 cv 9
4 4 dsd 10
[1] "Original column names "
[1] "First" "Second" "Third"
[1] "Modified DataFrame : "
ColFirst ColSecond ColThird
1 1 a 7
2 2 ab 8
3 3 cv 9
4 4 dsd 10