📜  如何在R中获取给定DataFrame的结构?

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

如何在R中获取给定DataFrame的结构?

在本文中,我们将看到如何在 R 编程中获取 DataFrame 的结构。

获取DataFrame结构的步骤:

  • 创建数据框。
  • 每个向量的大小应该相同。
  • 创建数据框时遵循语法。

函数:

为了获取数据框的结构,我们使用了一个名为 srt() 的内置函数。

示例 1:创建具有 2 列的数据框:

R
df1 = data.frame(id = c(1  ,2 , 3), name = c("karhik" , 
                                           "nikhil" ,
                                           "sravan"))
print(df1)


R
df2 = data.frame(sid = c(1, 2, 3),
                 sname = c("karthik" , "nikhil" , "sravan"),
                 Branch = c("IT" , "CSE" , "IT"))
print(df2)


R
df3 = data.frame(eid = c(1, 2, 3) ,
                 ename = c("krishna" , "nikhil" , "manoj"),
                 salary = c(50,000 , 60,000 , 70,000), 
                 Designation = c("senior manager" , "HR" , "Manager"))
print(df3)


R
df1 = data.frame(id = c(1  , 2, 3),
                 name = c("karhik" , "nikhil" , "sravan"))
srt(df1)


R
df2 = data.frame(sid = c(1, 2, 3),
                 sname = c("karthik" , "nikhil" , "sravan"),
                 Branch = c("IT" , "CSE" , "IT"))
str(df2)


R
df3 = data.frame(eid = c(1, 2, 3) , 
                 ename = c("krishna" , "nikhil" , "manoj"),
                 salary = c(50000 , 60000 , 70000),
                 Designation = c("senior manager" , "HR" , "Manager"))
str(df3)


输出:

示例 2:创建具有 3 列的数据框。

电阻



df2 = data.frame(sid = c(1, 2, 3),
                 sname = c("karthik" , "nikhil" , "sravan"),
                 Branch = c("IT" , "CSE" , "IT"))
print(df2)

输出:

示例 3:创建具有 4 列的数据框。

电阻

df3 = data.frame(eid = c(1, 2, 3) ,
                 ename = c("krishna" , "nikhil" , "manoj"),
                 salary = c(50,000 , 60,000 , 70,000), 
                 Designation = c("senior manager" , "HR" , "Manager"))
print(df3)

输出:

得到一个给定的数据帧的结构

示例 1:df1 的结构

电阻

df1 = data.frame(id = c(1  , 2, 3),
                 name = c("karhik" , "nikhil" , "sravan"))
srt(df1)

输出:



示例 2:df2 的结构

电阻

df2 = data.frame(sid = c(1, 2, 3),
                 sname = c("karthik" , "nikhil" , "sravan"),
                 Branch = c("IT" , "CSE" , "IT"))
str(df2)

输出:

示例 3:df3 的结构

电阻

df3 = data.frame(eid = c(1, 2, 3) , 
                 ename = c("krishna" , "nikhil" , "manoj"),
                 salary = c(50000 , 60000 , 70000),
                 Designation = c("senior manager" , "HR" , "Manager"))
str(df3)

输出: