R: avoid repeating $ - r

I'm new here and new to R and I think I have a simple question but don't know how to name it so I can't find any help by searching the web.
I have a data set and want to form a new Data set with several variables from the first one.
The working code looks like this:
em.table2 <- data.frame(em.table$item1,em.table$item2,...[here are some more]...,em.table$item22)
In order to keep it more simple, I want to get rid of the "em.table$"-construction in front of every variable... unfortunately i don't know the function to do so...
I tried it like this, but it didn't work (and is a pretty embarrasing try i guess):
em.table2 <- data.frame(em.table$(item1,item2,item3,item4))
Anyone here to help? Thanks a lot!

Instead of the $ operator, try the following:
em.table2 <- em.table[,c("item1","item2","item3","item4")]

Try with
em.table2 <- with(em.table, data.frame(item1, item2, item3, item4))
But if you just want to subset the data, there are better solutions.

Related

R programming (beginner struggles): How can I make this list more efficiënt

I just started programming for the first time. I work on a research project with the tuber library. I already set up my api, this works fine.
Now I wanted to get my data into a table.
Therefore I want to combine 'get_stats' and 'get_video_details'.
To use the function 'get_stats' I need a 'video id'. Because I will work with 500 -1000 video Id's, I'm trying to find a way to overcome this:
my_list <- list(
vid_list$items[[1]]$id,
vid_list$items[[2]]$id,
vid_list$items[[3]]$id,
vid_list$items[[4]]$id,
vid_list$items[[5]]$id
......
)
View(my_list)
I figured out I will probably need a to use a loop. But I haven't found a way to work it out. Can anyone of you help me? It would be very much appreciated :)
We extract the 'items', loop through the list with lapply and extract the "id"
lapply(vid_list$items, "[[", "id")

How to print particular JSON value in R?

install.packages("rjson")
library("rjson")
result <- fromJSON(file="provdata.json.txt")
for (i in seq(1,5,1))
{
term <- result$data[i]
print(term)
}
I was wondering what should I put to only print 13.6 and 4.15. Thank you!
I can't check for sure the structure of the json list, as you don't provide a reproducible example (please check https://stackoverflow.com/help/how-to-ask), but I think your problem is that you're not subsetting the list deep enough:
instead of the
term <- result$data[i]
print(term)
bit, try with
print(result$data[[1]][[1]][2])
or something similar. Alternatively, please use dput on a subset of your result variable, so we can help better.

How to specify the date format of column in Rscript

I have tried this. But its giving me error
Datecreated<-c('Created Time')
I will get this data from cloud using APIs. I need to define the format of this Created Time column as d-mon-yy
For example- 18-Nov-17
How can I achiEve this. I am new to R.
Any help woud be appreciated.
Your screenshot is showing you assign the string ‘created time’ to the variable Datecreated. I’m not sure where or how you’re getting your data but I’m assuming that is your issue. Once you have your data you can use akrun’s answer of as.Date(column, “%d-%b-%y”)
MyDates <- c(“17-Jan-16”, “6-Feb-17”)
FormattedDates <- as.Date(MyDates, “%d-%b-%y”)

Difficulties adding data to R dataset

I'm not too advanced with R so any help would be appreciated. I am trying to add values to columns in my dataset and my dataset is called 'katie'.
For example, in the column 'word' I'd like to select instances where 'SUBJECTED' is written and then post 'middle' in the column 'pre.environment', on the same line as 'SUBJECTED' is written. Is there something that I am doing wrong? With this code, the initial line definitely works (as I can see how many "SUBJECTED" items are recognized in the column 'word') but nothing happens when I enter the second line of code.
>x=grep("SUBJECTED", katie$word)
>katie[x,]$pre.environment= c('middle')
I hope this example is sufficient. Thanks in advance for your help.
Try the following code, if I understand your question correctly,
katie$pre.environment <- ifelse(grepl("SUBJECTED", katie$word),
yes = "middle",
no = katie$pre.environment)

exporting plotted variable shows blank image

I am doing java and R integration using JRI.
Please find below script
String path = "C:\\Users\\hrpatel\\Desktop\\CSVs\\DataNVOCT.csv";
rengine.eval("library(tseries)");
rengine.eval(String.format("mydata <- read.csv('%s')",path.replace('\\', '/')));
String exportFilePath= "C:\\Users\\hrpatel\\Desktop\\CSVs\\arima3.jpg";
rengine.eval("Y <- NewVisits");
rengine.eval("t <- Day.Index");
rengine.eval("summary(Y)");
rengine.eval("adf.test(Y, alternative='stationary')");
rengine.eval("adf.test(Y, alternative='stationary', k=0)");
rengine.eval("acf(Y)");
rengine.eval("pacf(Y)");
rengine.eval("mydata.arima101 <- arima(Y,order=c(1,0,1))");
rengine.eval("mydata.pred1 <- predict(mydata.arima101, n.ahead=1000)");
rengine.eval(String.format("jpeg('%s')",exportFilePath.replace('\\', '/')));
rengine.eval("plot(t,Y)");
rengine.eval("lines(mydata.pred1$pred, col='blue',size=10)");
rengine.eval("lines(mydata.pred1$pred+1*mydata.pred1$se, col='red')");
rengine.eval("lines(mydata.pred1$pred-1*mydata.pred1$se, col='red')");
rengine.eval("dev.off()");
In above codebase when i tried plot(t,Y) or plot(Y). it export a blank image, while in case of plot(mydata) it is working file.
One more thing when i run above code in R it creates the image(using JRI it shows blank image).
I have spend 1 day to solve this but i dont found any solution.
Please suggest if you have any alternatives.
Your help is needed.
Thanks in Advance
if i understand correctly, you have a data set named mydata, that has two columns, NewVisits, and Day.Index, in that case you need to change:
rengine.eval("Y <- NewVisits");
to
rengine.eval("Y <- mydata$NewVisits");
and
rengine.eval("t <- Day.Index");
to
rengine.eval("t <- mydata$Day.Index");
This also explains why plot(mydata) works for you - because R recognizes it.
if this isn't the solution, then i cant see where you are reading NewVisits and Day.Index from
BTW i stongly recommend to plot using the ggplot package

Resources