Reputation: 15
First off, I'm new to R, so this question won't be using if statements or anything specific. I loaded an xls document into R, and now have to open a specific sheet and call specific columns from one particular sheet. Let's call the sheet "fruit" and the columns: "apples", "oranges", "bananas". I also have to skip the first two rows, so I might have done that wrong as well. Please let me know if I did. I'm trying this :
fruit_types <-read_excel('fruittypes.xls', sheet = "Fruit",
col_names = "apples","oranges","bananas" ,skip = 2)
It just isn't working. And no, I am not allowed to just select columns, I have to call them by their name.
Upvotes: 1
Views: 3478
Reputation: 1
So basically, I'm pretty sure you'll have to manually assign each name whenever you're using the col_names parameter in read_excel. Even I tried "skipping" re-naming each of the column names but you'll have to either rename all of them or just name the column you want and just type in the original column names within the c("","",..)
Even I'm kind of new to R so that's what I've learnt until now but if anyone knows this better they can please let us know xD.
Try fruit_types <-read_excel('fruittypes.xls', sheet = "Fruit", col_names = c("apples","oranges","bananas"),skip = 2)
so in your case just do:
fruit_types <-read_excel('fruittypes.xls', sheet = "Fruit", col_names = c("changedname1","changedname2","bananas"))
the 'skip=' works only with col_types parameter
or if u want to manually change the first two using a colnames function u can do;
colnames(fruit_types)[colnames(fruit_types) == "apples"] <- "DesiredName"
Hopefully this helps
Upvotes: 0
Reputation: 3
You could try using "range" instead. E.g., using the raw excel sheet created by Arun kumar mahesh the code would be as follows:
setwd ("folderpath")
library(readxl)
data <- read_excel("data.xlsx", sheet = "Fruits", range: "A3:E6")
Upvotes: 0
Reputation: 2359
To reproduce the sample example for your understanding have created a raw excel sheet
setwd ("folderpath")
library(readxl)
data <- read_excel("data.xlsx", sheet = "Fruits", skip = 2)
data <- subset (data , select = c("Apple","Banana","Oranges"))
print (data)
# A tibble: 3 x 3
Apple Banana Oranges
<dbl> <dbl> <dbl>
1 1 4 7
2 2 5 8
3 3 6 9
Upvotes: 2