Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have my data in xls file.I try to read like this
> df = read.xls ("natgas.xls")
Output
df
Dec.2007 X2399154
1 Jan-2008 2733970
2 Feb-2008 2503421
3 Mar-2008 2278151
4 Apr-2008 1823867
5 May-2008 1576387
6 Jun-2008 1604249
7 Jul-2008 1708641
8 Aug-2008 1682924
9 Sep-2008 1460924
10 Oct-2008 1635827
Everything is OK,except the first line.
When I index second column
> df[,2]
[1] 2733970 2503421 2278151 1823867 1576387 1604249 1708641 1682924 1460924
the first value is missing.
How to solve this?
Looks like you need to add header = FALSE to your read.xls call (which seems to come from the gdata package):
df1 <- read.xls("natgas.xls", header = FALSE)
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am using mean function in R. But getting different answers all the time, I change the sequence of the inputs.
Not able to understand. Please help me.
Thanks in advance.
> mean(10,12,13)
[1] 10
> mean(12,10,13)
[1] 12
> mean(13,10,12)
[1] 13
You should pass numbers as a vector to mean and not as separate values. See ?mean.
mean(x, ...)
So when you are doing mean(10,12,13) you are just getting mean of 10 hence the same number is returned. Same with mean(12,10,13).
Pass them as a vector with c(...).
mean(c(10,12,13))
#[1] 11.66667
mean(c(12,10,13))
#[1] 11.66667
This is different behaviour from functions like sum/min/max where you can pass different numbers as comma separated values.
sum(10, 12, 13)
#[1] 35
min(10, 12, 9)
#[1] 9
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
> df <- read.csv("DATA ONLY.csv", header = TRUE, sep = ";")
> dim(df)
[1] 439 1
This is the code I use and this is the CSV
https://docs.google.com/spreadsheets/d/1SOqDKXZ7BAMW5LdqBcBIvQE9_PnFcNIHDfYDty3cTto/edit?usp=sharing
I am 99% sure that you have defined the field separator wrong. data.table::fread is really good at sniffing the correct format of csv's, and I quite often use fread even if I just convert the resulting data.table back to vanilla data frame, i.e.
library(data.table)
df <- fread("DATA ONLY.csv")
as.data.frame(df) -> df
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have a data frame in R that has two columns, one with last names, the other with the frequency of each last name. I would like to randomly select last names based on the frequency values (0 -> 1).
So far I have tried using the sample function, but it doesn't allow for specific frequencies for each value. Not sure if this is possible :/
df1 <- data.frame(names = c("John","Mary"),freq=c(0.2,0.8))
df1
# names freq
# 1 John 0.2
# 2 Mary 0.8
set.seed(1)
sample100 <- sample(
x = df1$names,
size = 100,
replace=TRUE,
prob=df1$freq)
table(sample100)
# sample100
# John Mary
# 17 83
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have a text file myfile.txt that looks like this:
10
20
30
40
I try to load it in R using:
nums <- read.csv('myfile.txt', header=FALSE);
However, this returns a list. What I want is the equivalent of:
nums <- c(10, 20, 30, 40);
I later have code that does the following:
v = sprintf("%d", nums);
This works fine when I have the vector version, but when I try to load my data from file, I get the following error:
Error in sprintf("%d", nums) : unsupported type
Execution halted
Does nobody use scan anymore??
> scan("./myfile.txt")
Read 4 items
[1] 10 20 30 4
I mistyped the fourth item in the file.
Returns exactly what you wanted:
> n = scan("./myfile.txt")
Read 4 items
> identical(n, c(10,20,30,4))
[1] TRUE
When you use read.csv, the output should be a data.frame.
If you want to access the column of integers, try nums[[1]]. (With nums being the output read.csv.)
To convert to a vector, use unlist(nums).
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I want to add a first column with consecutive numbers with characters in a existing data frame.
I use the following code. It does not work.
df$VARNAME_ <- paste0('COL', 1:5)(df)
I want to it look like this.
VARNAME_ old_var1 old_var2
COL1 1 2
COL2 1 2
COL3 1 2
COL4 1 2
COL5 1 2
Thanks in advance.
I am Sorry that I asked a stupid question. And now I figure out.
The solution is as following.
actual_df<-data.frame(df)#transfer matrix a to data frame
actual_df<-cbind(VARNAME_=paste0('COL', 1:5),actual_df) #add COL1~COL5 in the first column
actual_df<-cbind(ROWTYPE_ = 'PROX', actual_df) #Add a variable with constant observations in first column. Now the previous column become second one.
df$VARNAME_ = paste0('COL', 1:5)
will work