将列移动到 R 中 DataFrame 的第一个位置
在本文中,我们将看到如何在 R 编程语言中将数据帧中的特定列移动到数据帧的第一个位置。
创建用于演示的数据框:
R
# create a dataframe with 4 columns
# they are id,name,age and address
data = data.frame(id=c(1, 2, 3),
name=c("sravan", "bobby",
"satwik"),
age=c(23, 21, 17),
address=c("kakumanu", "ponnur", "hyd"))
# display
data
R
# create a dataframe with 4 columns
# they are id,name,age and address
data = data.frame(id = c(1,2,3),
name = c("sravan","bobby",
"satwik"),
age = c(23,21,17),
address = c("kakumanu","ponnur","hyd"))
# display
print("Original Dataframe")
data
print("After moving age to first column : ")
# move age to first column
data_Sorted = data[ , c("age",
names(data)[names(data) != "age"])]
# display sorted data
data_Sorted
print("After move address to first column : ")
# move address to first column
data_Sorted1 = data[ , c("address",
names(data)[names(data) != "address"])]
# display sorted data
data_Sorted1
R
# load the dplyr package
library("dplyr")
# create a dataframe with 4 columns
# they are id,name,age and address
data = data.frame(id = c(1,2,3),
name = c("sravan","bobby",
"satwik"),
age = c(23,21,17),
address = c("kakumanu","ponnur","hyd"))
# display
print("Original Dataframe")
data
print("After moving age to first column : ")
# move age to first column
data_Sorted = data %>% dplyr::select("age",
everything())
# display sorted data
data_Sorted
输出:
方法 1:使用基础 R
在此方法中,我们将使用基本 R 语言将列移动到第一个位置。
Syntax: dataframe[ , c(“column_name”, names(dataframe)[names(dataframe) != “column_name”])]
where
- dataframe is the input dataframe
- column_name is the name of the column to be shifted to the first
我们将使用索引运算符和 c()函数将特定列移动到第一个位置,我们将组合列名,然后推送到数据框中的第一个位置。
示例: R 程序将上面创建的数据帧的列移到第一个。
电阻
# create a dataframe with 4 columns
# they are id,name,age and address
data = data.frame(id = c(1,2,3),
name = c("sravan","bobby",
"satwik"),
age = c(23,21,17),
address = c("kakumanu","ponnur","hyd"))
# display
print("Original Dataframe")
data
print("After moving age to first column : ")
# move age to first column
data_Sorted = data[ , c("age",
names(data)[names(data) != "age"])]
# display sorted data
data_Sorted
print("After move address to first column : ")
# move address to first column
data_Sorted1 = data[ , c("address",
names(data)[names(data) != "address"])]
# display sorted data
data_Sorted1
输出:
方法 2:使用 dplyr() 包
通过使用这个包,我们可以将特定列移动到第一列,这里我们使用 %>%运算符将移动后的数据加载到数据帧中,并使用 select()函数将特定列名进行移动,而 every() 是用于获取数据框数据
Syntax: dataframe%>% dplyr::select(“column_name”, everything())
where
- dataframe is the input dataframe
- column_name is the column to be shifted to first
示例:将特定列移至第一列的 R 程序
电阻
# load the dplyr package
library("dplyr")
# create a dataframe with 4 columns
# they are id,name,age and address
data = data.frame(id = c(1,2,3),
name = c("sravan","bobby",
"satwik"),
age = c(23,21,17),
address = c("kakumanu","ponnur","hyd"))
# display
print("Original Dataframe")
data
print("After moving age to first column : ")
# move age to first column
data_Sorted = data %>% dplyr::select("age",
everything())
# display sorted data
data_Sorted
输出: