I have a set of csv data that is saved in matrix format attached image is an example of the matrix
I would like to load the data into R and have it stored as a data frame with x$Year,x$Death,x$ASMR. How would I be able to do that?
Thanks!
CS
I think you're just looking for read.csv() and then change the colnames. I am assuming your file is separated by commas.
x <- read.csv('matrix.csv', sep=',', header=T)
colnames(x) <- c('Year', 'Death', 'ASMR')
Related
Im having trouble by converting my xlsx file to a data frame.
First: I do not know how to set the first column as rownames and the first row as col.names (because the first column are latitude values and the first row are longitude values). But THE MAIN issue is that I want each sheet of the xlsx to be part of the third dimension.
I want the dimension of my data frame to be c(length(latitude),length(longitud),35) becaused I got an xlsx with 35 sheets.
How could I do that?
I used this code to get my xlsx file. It worked but when I try to convert it to a data frame called CCSM4 it failed. It has more than 10.000 columns and latitude and longitude are values of my dataframe instead of being just col and rownames.
lst <- lapply(1:35, function(i) read_excel("CCSM4-MATRIZ.xlsx", sheet = i))
CCSM4<-as.data.frame(lst)
This is how ONE sheet of my xlsx looks like:
Data frames (special class of list type) only maintains two dimensions. However, R's array (where at 2-D is equivalent to matrix) can have N-dimensions. Hence, consider converting your data frame to matrix and with sapply (wrapper to lapply) can convert to three-dimensional array.
lat_lon-array <- sapply(1:35, function(i)
matrix(read_excel("CCSM4-MATRIZ.xlsx", sheet = i)),
simplify = "array")
dim(lat_lon-array)
I am reshaping my question,
I need to import several CSV files of 296x2 (296 rows x 2 columns) into a single dataset.
First column is the same for all files.
I would like to merge all the CSV into a single dataset columnwise (conserving only the first column as row name once.
In other words, All the 329 CSV files are comma delimited and are all the same 296x2. I would like to end up with a 296x329 dataset that includes the second column of each dataset.
Thanks in advance
Emiliano
Without knowing your data it's difficult to say, but assume you have your dataset in a folder name: C:/foo/. Try this one:
filenames <- list.files('C:/foo/', pattern="*.csv", full.names=TRUE)
la <- lapply(filenames, read.csv)
Reduce(function(x, y) merge(x, y, by="Wavelength"), la)
I need to import data from csv file and later do computations with it as with usual matrix in R. Data in csv file contain only numbers except variable names in header. I used following commands:
XX <- read.table("C:/Users/.../myfile.csv", header = TRUE)
and got something resembling a matrix, with numbers separated with comas. Then:
X<- as.matrix(sapply(XX, as.numeric))
which gave me just column vector with strange numbers. What am i doing wrong? Thanks for help!
Is it possible to read multiple csv excell files into R. All of the csv files have the same 4 columns. the first is a character, the second and third are numeric and the fourth is integer. I want to combine the data in each numeric column and find the mean.
I can get the csv files into R with
data <- list.files(directory)
myFiles <- paste(directory,data[id],sep="/")
I am unable to get the numbers from the individual columns add them and find the mean.
I am completely new to R and any advice is appreciated.
Here is a simple method:
Prep: Generate dummy data: (You already have this)
dummy <- data.frame(names=rep("a",4), a=1:4,b=5:8)
write.csv(dummy,file="data01.csv",row.names=F)
write.csv(dummy,file="data02.csv",row.names=F)
write.csv(dummy,file="data03.csv",row.names=F)
Step0: Load the file names: (just like you are doing)
data <- dir(getwd(),".csv")
Step1: Read and combine:
DF <- do.call(rbind,lapply(data,function(fn) read.csv(file=fn,header=T)))
DF
Step2: Find mean of appropriate columns:
apply(DF[,2:3],2,mean)
Hope that helps!!
EDIT: If you are having trouble with file path, try ?file.path.
I am importing a csv of stock data into R, with column names of stock ticker which starts with number and containing space inside, e.g. "5560 JP". After reading into R, the column names are added with "X" and space replaced by ".", e.g. "X5560.JP". After all the works are done in R, I want to write the processed data back to a new csv, but with the original column name, e.g. "5560 JP" instead of "X5560.JP", how can I do that?
Thank you!
When you use write.csv or write.table to save your data to a CSV file, you can set the column names to whatever you like by setting the col.names argument.
But that assumes you have the column names to available.
Once you've read in the data and R has converted the names, you've lost that information. To get around this, you can suppress the conversion to get the column names:
df <- read.csv("mydata.csv", check.names=FALSE)
orig.cols <- colnames(df)
colnames(df) <- make.names(colnames(df))
[your original code]
write.csv(df, col.names=orig.cols)