Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Excel文件导入到R_R - Fatal编程技术网

将Excel文件导入到R

将Excel文件导入到R,r,R,我必须将Excel文件导入到R中,但到目前为止,我找到的每个教程都是关于简单的数据表的,而我的则要复杂一些。你能帮我吗 提前非常感谢 根据需要执行导入的频率,有两个选项。最简单的方法是跳过前几行,不带任何标题地读取数据,然后在R中手动创建列名。试试这个,如果需要更多帮助,请修改您的问题。您需要指定用于从Excel导入的包。此问题引发了有关MetaStackoverflow的讨论。您应该阅读,因为它可以解决您的问题,并且可以让您了解StackOverflow社区是如何工作的。 library(x

我必须将Excel文件导入到R中,但到目前为止,我找到的每个教程都是关于简单的数据表的,而我的则要复杂一些。你能帮我吗


提前非常感谢

根据需要执行导入的频率,有两个选项。最简单的方法是跳过前几行,不带任何标题地读取数据,然后在R中手动创建列名。试试这个,如果需要更多帮助,请修改您的问题。您需要指定用于从Excel导入的包。此问题引发了有关MetaStackoverflow的讨论。您应该阅读,因为它可以解决您的问题,并且可以让您了解StackOverflow社区是如何工作的。
library(xlsx)
library(zoo)

# Read the dataset starting form the 3rd line
df <- read.xlsx("SO.xlsx", 1, header=TRUE,startRow=3, stringsAsFactors=FALSE)

# Clean the data to remove the lines that should not be there
# like the lines 4 and 66 in this dataset
# this could be done many ways. Here I assume that all columns starting from the third 
# should have some values
df <- df[!is.na(df$hallos),]

# Assign the names to the first 2 columns
names(df)[1:2] <- c( "year", "type")

# The last 2 rows are summaries, so we probably want to remove them
df <- df[!grepl("",df$type),]

# The first column "year" has many missing values. We need to add year values to each cell:
df$year <- na.locf(df$year)
# Result
head(df)
#   year type  hallos  slyos.   knny sszesen  meghalt  slyosan  knnyen  sszesen.1
# 2 2013    J      28     255    622      905      33      300     870       1203
# 3 2013    F      31     223    527      781      34      248     764       1046
# 4 2013    M      34     274    691      999      34      320     971       1325
# 5 2013    A      36     349    757     1142      42      392    1090       1524
# 6 2013   Mj      52     436    902     1390      54      501    1241       1796
# 7 2013    J      39     455   1004     1498      41      509    1414       1964