Reading specific columns and rows from excel sheet in R [closed] - r

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have a large xlsx file called Run.xlsx. Inside this are multiple sheets and I want the sheet called "Factors". I also want to extract specific rows and columns from the factors sheet which are columns Z:AB and rows 15:71.
I have tried using the readxl package however it doesn't work for me.

If your Excel file columns order may change, it would be best to have an automatic code instead of selecting the columns number.
You could try
1- importing your xlsx file with "read.xlsx" function from "openxlsx" library
2- selecting columns with specific name
#1-import
library(openxlsx)
yourFile <- read.xlsx("yourPathway/yourFile.xlsx", sheet="yourSheet")
#2-columns selection
vectorNameColumns <- c("Age", "BMI", ..., "Gender")
vectorNameRows <- 15:71
refinedFile <- yourFile[vectorNameRows, vectorNameColumns]
It would also be best (for safety and time consuming purpose) to automatically select specific row names or IDs instead of row numbers in the case where your Excel file would be modified or if you want to apply the same code to another Excel file.

Related

How to create a CSV-File for a dataframe with multiple lists in R? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
As I'm dealing with a huge dataset I had to split my data into different buckets. Thus, I want to save some interim results in a csv to recall it later. However, my datafile contains some columns with lists, which according to R can not be exported (see snapshot). Do you guys know a simple way for a R newbie to make this work?
Thank you so much!
I guess the best way to solve your problem is switching to a more apropriate file format. I recomend using write_rds() from the readr package, which creates .rds files. The files you create with readr::write_rds('your_file_path') can be read in with readr::read_rds('your_file_path').
The base R functions are saveRDS() and readRDS() and the functions mentioned earlier form the readr are just wrappers with some convience features.
Just right click, then choose new csv to the folder where you want to save your work. Then set the separator of the csv to a comma.
Input all data in column form. You can later make it a matrix in your R program.

Convert Factors to Numbers But Getting N/As for The Entire Column [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
First off, I have looked at all the examples and previous questions and have not been able to find a usable answer to my situation.
I have a data set of 300ish independent variables I'm trying to bring into R. The variables are all classified as factors. In my csv file I'm uploading, all of the variables are pricing data with two decimal places. I have used the following code and some of the variables have been converted with decimals. However, many of the converted columns are filled with NAs; in fact, some entire columns are completely NAs.
dsl$price = as.numeric(as.factor(dsl$price)) # <- this completely changes the data into something unrecognizablbe
dsl$price = as.numeric(as.character(dsl$price)) # <- lots of NAs or totally NAs
I've tried to recode the variables in the original CSV file to numeric, but with no luck.
Convert the factor into character which can then be converted into numeric
dsl$price <- as.numeric(as.character(dsl$price))

Splitting a character into separate words in R [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am working on a project in R (on TED_Talks data set). I have a data frame with one column called "tags" which contains a character like
"gaming,gender,sex,feminism,education,culture".
The problem is, the whole row is being read as a single character.
I want the output to be a vector containing separate words. eg:
"gaming","gender","sex","feminism","education","culture"
so I can do further analysis on tags.
You can simply do the following:
say your entry is in object a, and you want to allocate the final result to object b:
a <- "gaming,gender,sex,feminism,education,culture"
b <- unlist(strsplit(a, "[,]"))

How to merge three .RData files with same object name in R [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am a new user of R. I have three RData files with the same object name and I want to merge it so that I will have one .Rdata file with one object name.
Example:
file1.RData with object name A
file2.RData with object name A
file3.RData with object name A
and Result should be
file.RData = object A
I tried rbind and merge command, but nothing is working.
You will need to save them as new objects as you load each, then do your merging. For example, here, create a list to hold the objects. Then, as you load each, add that version to your list.
listForFiles <- list()
load("file1.RData")
listForFiles[[1]] <- A
load("file2.RData")
listForFiles[[2]] <- A
load("file3.RData")
listForFiles[[3]] <- A
Then, you can use listForFiles to do your merging. Since you don't say what kind of object these are, I can't suggest an approach.
This is why you might want to consider saving your files as .RDS format. It is similar to .RDA, but it only saves one object at a time (with the saveRDS() command). To read then, you can use the readRDS() function and assign the object to whatever variable name you want. This is specially useful for large projects where you may have lots of data frames with generic names and eventually want to load them in a common script. It will save some time!

Factors R not fun: pulling labels vs levels [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm loading a csv file that contains colleges and their conferences into R. When I read the file and create a data frame, it automatically makes the conferences class factor. All I want is to pull the conference but I can only pull the "levels" being random numbers. When I use as.character it stores the random numbers. Can anyone assist me with this?
the following issue has taken me so long to make zero progress so I'd greatly appreciate guidance / assistance.
> data <- read.csv("Regression Data Working File.csv",stringsAsFactors = FALSE)
# the file is essentially just a list of colleges in one column and their corresponding conference in the other column
> class(data$conference) # is a vector of college conferences (SEC, ACC, etc.)
[1] "character"
> data$conference[2]
[1] "7" # should be "ACC" and it is "ACC" when I use View(data)
Ok, here's what I did to fix this. My original file had the column of conferences populated using a vlookup but I made sure to copy and paste these results as values (not knowing if the vlookup function instead of the data would impact the data in the csv file / r). In response to the comment above to provide a sample data file, I copied and pasted the values into a new excel file and tried that data in r and it worked. So I went back to my previous data file and deleted the vlookup data array in a different sheet to try to find an explanation and that resolved the issue. So my guess is that something about the conversion from an excel file into a csv file used the data array that was used for the vlookup and stored the values as that. Thanks for your help in troubleshooting this! Have a great weekend
Thanks,
OP

Resources