This question already has answers here:
Read a list of files with R, each file contains a list of float numbers. what's the proper way to do it?
(2 answers)
Closed 9 years ago.
I've got many text files with named by year i.e. yob1940.txt,yob1941.txt. Each file has 3 colums of data. I'm trying to import the data into R in a single data table, and add the year for each file in a 4th column.
Any help would be much appreciated.
Thanks
Smth like this will work:
rbindlist(lapply(list.files(pattern = "yob[0-9]+\\.txt"),
function(x) data.table(year = sub('.*?([0-9]+).*', '\\1', x),
fread(x)))))
Assuming you have read these files as x1 and x2
df.list<-list(x1,x2)
kk<-do.call(rbind,df.list)
year<-data.frame(rep(c(1940,1941),c(nrow(x1),nrow(x2))))
names(year)<-"year"
mydata<-data.frame(cbind(kk,year))
A sample example:
x1<-data.frame(x=c(1,3),y=c(2,3))
x2<-data.frame(x=c(3,3),y=c(2,2))
df.list<-list(x1,x2)
kk<-do.call(rbind,df.list)
year<-data.frame(rep(c(1940,1941),c(nrow(x1),nrow(x2))))
names(year)<-"year"
mydata<-data.frame(cbind(kk,year))
mydata
x y year
1 1 2 1940
2 3 3 1940
3 3 2 1941
4 3 2 1941
Related
This question already has answers here:
Convert currency with commas into numeric
(4 answers)
Closed 1 year ago.
I am working on a dataset that has 2 Price formatted variables that are currently read as character format. I need them to be numerical. I have tried the following different examples but all have created more than a thousand nas when I run it.
dataframe$Price <-as.numeric(dataframe$Price)
dataframe$Price <-as.numeric(as.character(dataframe$Price))
If I run it as
as.numeric(dataframe$Price)
It doesn't change the variable. I am relatively new to R (about 2 months) and I have no idea what I'm doing. I appreciate any help!
If every elements of dataframe$Price is like $12.12, for example,
dummy <- data.frame(
Price = c("$12.11", "$11.14", "$10.12")
)
Price
1 $12.11
2 $11.14
3 $10.12
By using stringr::str_replace function,
dummy$Price <- as.numeric(str_replace(dummy$Price, "\\$", ""))
Price
1 12.11
2 11.14
3 10.12
This question already has answers here:
Extracting unique numbers from string in R
(7 answers)
Closed 2 years ago.
I have a date data which have different input format. I would like to keep only number for it. what should I do.
The data looks like this:
The codes are:
Days<-c("Day 1","Day 4"," Day_6", "Day7")
Sample.data <- data.frame(Days)
Basicly I want to get the number out of Days. Was thinking use 'str_replace' or 'gsub' but don't know how to handle different pattern. Please give me as many methods as possible for this problem. Thanks.
Does this work:
> Sample.data$Day <- as.numeric(gsub('(.*)(\\d)', '\\2', Sample.data$Days))
> Sample.data
Days Day
1 Day 1 1
2 Day 4 4
3 Day_6 6
4 Day7 7
>
This question already has answers here:
How can I read a matrix from a txt file in R?
(2 answers)
Closed 7 years ago.
I have a very big file like this (no separator between characters):
1234
3456
2345
I want to read it to R as a matrix and get this:
1 2 3 4
3 4 5 6
2 3 4 5
This question is like this question: read in matrix into r without delimination but I am looking for a better way. I do not want to put the number of columns - I want the number of columns to be a variable in the code and support big files.
How about:
library(readr)
my_file <- "big_file.txt"
my_matrix <- as.matrix(read_fwf(my_file, fwf_widths(rep(1,nchar(readLines(my_file, n=1))))))
nchar(readLines(my_file, n=1)) reads the first line and counts the number of characters. This is the multiplier of for the rep() for specifying the fwf_widths.
This assumption being that all your numbers are integers between 0 and 9.
This question already has answers here:
Quick question about subsetting via character-class
(3 answers)
Closed 8 years ago.
I have a vector called gas
gas <- c("Hydrogen","Methane")
I also have a data frame called df that looks like
df <- ID Hydrogen Methane
1 2 20
1 3 19
1 2 23
2 8 13
ect..
Normally to use a variable in a data frame I would use df$Hydrogen for example but what I want to know is can I also call Hydrogen by using the vector above? e.g.
data$gas[1]
#In other words I would like the following to be true:
data$gas[1] == data$Hydrogen
what syntax, if any, would I use to obtain this?
Thanks
If you want a specific gas, try:
df[,gas[1]]
For all gases:
df[gas]
This question already has answers here:
Convert columns to rows keeping the name of the column
(2 answers)
Closed 9 years ago.
I have the following data:
word Jan-2013 Feb-2013 Mar-2013
A 1 2 3
B 5 2 4
I want to convert the multiple date columns into one, named date and add an additional column for the value.
word date value
A Jan-2013 1
A Feb-2013 2
A Mar-2013 3
B Jan-2013 5
B Feb-2013 2
B Mar-2013 4
Can anyone assist?
Thanks
Additional R options
In addition to Metrics's answer, here are two additional options for R (assuming your data.frame is called "mydf"):
cbind(mydf[1], stack(mydf[-1]))
library(reshape)
melt(mydf, id.vars="word")
Excel option
I am not an Excel user, but since this question is tagged "Excel" as well, I would suggest the Tableau Reshaper Excel add-on.
For your example, it's pretty straightforward:
Go to the "Tableau" menu after installing the add-on and activating it.
Select the cells which contain the values you want to unstack. Click on OK.
View the result.
Using reshape from base R (df1 is your dataframe)
reshape(df1,times=names(df1)[-1],timevar="date",varying=names(df1)[-1],v.names="value",new.row.names=1:6,ids=NULL,direction="long")
word date value
1 A Jan.2013 1
2 B Jan.2013 5
3 A Feb.2013 2
4 B Feb.2013 2
5 A Mar.2013 3
6 B Mar.2013 4