2017-05-30 111 views
1

Qualtrics在第一行生成帶有變量名稱的csv文件,並在第二行生成變量標籤。我想使用read_csv()讀取我的數據,同時將第一行讀作列名,然後跳過下一行變量標籤。以下是我的失敗嘗試。如何跳過第二行是csv文件,同時使用read_csv將第一行保留爲列名?

library(readr) 
mydata <- read_csv("qualtrics_data.csv", col_names = TRUE, skip = 2) # this would actually skip both the names and label rows. 
+1

這幫助我解決了類似的問題:https://stackoverflow.com/questions/23543825/r-read-table-how-can- i-read-the-header-but-skip-lines – GlennFriesen

回答

4

您可以只讀兩次 - 一次獲取名稱,然後獲取數據。

library(readr) 
library(dplyr) 

csv_file <- "mpg,cyl,disp,hp,drat,wt 
mpg,cyl,disp,hp,drat,wt 
21.0,6,160,110,3.90,2.875 
22.8,4,108,93,3.85,2.320 
21.4,6,258,110,3.08,3.215 
18.7,8,360,175,3.15,3.440 
18.1,6,225,105,2.76,3.460" 


df_names <- read_csv(csv_file, n_max = 0) %>% names() 

df_names 
#> [1] "mpg" "cyl" "disp" "hp" "drat" "wt" 

df <- read_csv(csv_file, col_names = df_names, skip = 2) 

df 

#> # A tibble: 5 x 6 
#>  mpg cyl disp hp drat wt 
#> <dbl> <int> <int> <int> <dbl> <dbl> 
#> 1 21.0  6 160 110 3.90 2.875 
#> 2 22.8  4 108 93 3.85 2.320 
#> 3 21.4  6 258 110 3.08 3.215 
#> 4 18.7  8 360 175 3.15 3.440 
#> 5 18.1  6 225 105 2.76 3.460 
0

使用read.csv 如:

df <- read.csv("example.csv") 
df <- df[-1,] # -1 removes the first row, you can change to -2 to remove 2nd row...etc 
+1

由於第二行包含標籤,因此這樣做會導致所有列被解析爲字符變量 – austensen