for loop in data frame in R [closed] - r

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 am new in R and I would like to ask what is wrong in the following simple command. When i try:
for (k in 1:1){
logdata= logDataset[1:74+k,k]}
logdata collects the values from rows 2:75 of column 1 of the logDataset data frame. But when I use:
logdata=logDataset[1:75,1]
logdata collects correctly the first 75 values of the first column. Why the first command does not work as the second one? How can I use the "for" command for collecting the first 75 values of the first column? Many thanks.

Related

The length() function is not returning the right number [closed]

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 7 months ago.
Improve this question
I working with a csv that has 234 rows. I thought the length() function was supposed to return the number of rows in my csv, but it returns a much larger number. It gives me 1046860. Does the length function not do what I think it does? What is going on?
I just wanted to double check the length and haven't run any other code yet other than setting up my working directory and attaching a file (Cat6 <- read.csv("Category6.csv")).

Write an R function which accepts a list of integers and returns only the odd values in the original list [closed]

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 3 years ago.
Improve this question
Write an R function which accepts a list of integers and returns only the odd
values in the original list. R has a number of built-in datasets; one of them is
called Nile and a data frame containing these data is added to your working
environment with the command data(‘Nile’). How many of the entries in this
dataset are odd?
What I've written is
return.odd<-function(y)return(y[y%%2=1])
length(return.odd(Nile))
But it returns
Error: unexpected '=' in "return.odd<-function(y)return(y[y%%2="
and
Error in return.odd(Nile) : could not find function "return.odd"
Could you please tell where my mistakes are?
return_odd <- function(x) {
x[x %% 2 == 1]
}
length(return_odd(Nile))
[1] 27

How to choose the first "n" elements in R [closed]

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
(USING R)
So I imported a data set by using
xcars <- read.csv(file.choose())
and then I chose my data set which was originally an excel file.
So, I have a column named dist (short for displacement) and I want to choose the first 25 entries underneath that column and then plot it on a histogram, so I attempted the following.
carsUpTo25 <- xcars(1:25,)
hist(carsUpTo25$dist)
Of course this didn't work. However, any help on how I would do this would be helpful.
Try this-
hist(xcars[,dist[1:10]])

convert to date in R using abbreviated month [closed]

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'm new to R and trying to load some time series data but I'm stuck at the first hurdle.
I have a dataframe with a date column called Date. The date format of the data is: 23-May-16 (it appears like this in the R console when I print df). To read as date I'm trying:
df$Date <- as.Date(df$Date, "%dd-%bbb-%yy")
as per guidance here
which produces the value <NA> when it reads the data.
Try:
as.Date(df$Date,format="%d-%b-%y")
You only need to list those once:
as.Date("23-May-16", "%d-%b-%y")

Finding/Replacing "^" character using grep [closed]

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
Suppose I have a very simple data frame:
symbol <- c("A", "A^","B","C","C^")
df = data.frame(symbol)
Now imagine this is a very large dataframe so that I cannot easily list the rows in which the character "^" appears.
How can I subset the rows with (or without) that character?
Notice that things like:
df[grep("^", df$symbol)
or regular subsetting will not work since the carat "^" is usually used to represent the beggining of a string.
Many thanks
Add fixed to the grep function, as follows:
df[grep("^", df$symbol, fixed=T),]

Resources