📜  如何从 R 中的给定向量创建数据帧?

📅  最后修改于: 2022-05-13 01:55:13.421000             🧑  作者: Mango

如何从 R 中的给定向量创建数据帧?

在本文中,我们将看到如何从 R 中的四个给定向量创建数据帧。要使用向量在 R 中创建数据帧,我们必须首先拥有一系列包含数据的向量。 data.frame()函数用于从 R 中的向量创建数据框。

句法:

data.frame(vectors)

示例 1.从给定的 4 个向量创建数据帧。

R
# creating a vector with some value
id = c(1, 2, 3)
 
# creating another vector with some value
name = c("karthik" , "nikhil" , "sravan")
 
# creating another vector with some value
branch = c("IT" , "CSE" , "IT")
 
# creating another vector with some value.
favourite_subject = c("SE" ,"DAA" , "OS")
 
# passing the vectors into data.frame() function
# as parameters
df1=data.frame(id, name, branch, favourite_subject)
 
# printing the data frame.
print(df1)


R
# creatingvvector 1 with some values
faculty_id = c(247, 143, 01768)
 
# creating a vector 2 with some values
faculty_name=c("Subbarao", "praveen kumar", "sujatha")
 
# creating vector 3 with some values
designation=c("accociate professor", "assistant professor",
              "accosiate professor")
 
# creating vector 4 with some data
salary = c(60000, 50000, 60000)
 
# passing the vectors to the data.frame() function
df3 = data.frame(faculty_id, faculty_name, designation,salary)
 
# printing the data frame created with 4 vectors
print(df3)


输出:



示例 2:

电阻

# creatingvvector 1 with some values
faculty_id = c(247, 143, 01768)
 
# creating a vector 2 with some values
faculty_name=c("Subbarao", "praveen kumar", "sujatha")
 
# creating vector 3 with some values
designation=c("accociate professor", "assistant professor",
              "accosiate professor")
 
# creating vector 4 with some data
salary = c(60000, 50000, 60000)
 
# passing the vectors to the data.frame() function
df3 = data.frame(faculty_id, faculty_name, designation,salary)
 
# printing the data frame created with 4 vectors
print(df3)

输出: