Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I need to use this data.frame to analyse data Investment using package lattice.
Requirement is to use data.frame. Instead of the variable Investment I need to put variables which have influence to the Investment. I need to draw different graphs. I tried to draw one, but it's not what I need, because my code doesn't use data.frame at all.
library(lattice)
xyplot(Investment~GNP,data=Investment)
is.data.frame(Investment)
How can I change my code according to requirments? Thank you in advance.
data(Investment, package="sandwich")
Investment <- as.data.frame(Investment)
Here you have converted your data into data frame (as.data.frame)
if you wnat to access different columns in data frame you can select by using operator $
example:
Investment$column_name
plotting can be done by using plot function in which also you can select variables by using $ operator like
plot(Investment$column1,Investment$column2)
as.data.frame converts other formats of data into data frame
data.frame itself intializes a data frame
For creating new data frame you can use data.frame()
Hope it helps
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm new to R. I would like to know how I can create an array with two dimensions, the first being a numeric matrix and the second a character matrix?
Thanks.
An array cant have multiple datatypes. What you are looking for is the dataframe.
For example, you can create a dataframe with a numeric and a character column like this:
df <- data.frame(numericColumn=1:26,characterColumn=LETTERS)
To access these columns, you can use square brackets or the names. For more details, just read the documentation of dataframe or search for some examples.
If you really want to store matrices of different types in one object, you can use a list, for example like
matrixList <- list(numericMatrix=someNumericMatrix,characterMatrix=someCharacterMatrix)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have several data frames in my environment, beginning with SPECIALITY:
I would like to be able to only call the data frames once in my self-defined functions (possibly with an apply function), instead of having to run a line of code for each data frame like so:
I was thinking of combining the data frames into a list, but I'm not sure how I would go about doing this, or whether that would be the most efficient method.
Storing them into a list is an excellent idea, you can do it this way:
new_list <- mget(ls(pattern="^SPECIALTY"))
And then use lapply on it with the function of your choice.
If you want to clean up your workspace after you've put them in a list, run :
rm(list = ls(pattern="^SPECIALTY")))
To go further you might want to challenge why you got them in separated tables to start with, maybe it's because you've done something like:
SPECIALTY2014_Q1 <- read.csv("SPECIALTY2014_Q1.csv")
SPECIALTY2014_Q2 <- read.csv("SPECIALTY2014_Q2.csv")
...
In this case you could have done the following to store everything in a list from the start:
lapply(paste0("SPECIALTY", c("2014_Q1", "2014_Q2"),".csv"), read.csv)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm using R, trying to use data I've been given to plot a boxplot. One of the categories is "sex", with (obviously) either "M" or "F". How do I exclude females, so I can use the male data to create a boxplot? I'm new to R, any help would be appreciated!
EDIT: Okay, I managed to make a vector(??) only including male data using newdata<-subset(olympic, sex=="M"). Now how do I do the same but with two subsets of a different category of continous data? Is it similar notation? (E.g. say the category is "hair" with different colours, and I want "blonde" and "ginger")
Use boxplot(, subset = )
I don't have data, so only direct you to subset argument.
I think as a general guideline, check documentation page of an R function first before asking on SO. R functions, especially those producing summary plots, are very powerful as they often have many arguments offering very flexible options. See `?boxplot' in this case.
try to boxplot on male_data
male_data <- data[data$sex == "M",]
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to pass a data frame to a function as an argument. And then inside the function, I want to work on different combinations of columns for graphical presentation. Basically, I want to do graphical presentation on different data files. I want that, I pass the data file as an argument and then get the graphs. How can I do this in R.
You are not giving us much info but here is a very basic starting point:
library(ggplot2) # if you don't have this library run install.packages('ggplot2')
myAmazingFunction <- function(myDF) {
ggplot(myDF,aes(X,Y))+geom_line()
}
df <-data.frame(X=1:30, Y=runif(30), Z=1.3*runif(30))
myAmazingFunction(df)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am new to r and have a data set containing a column with 3 states (1,2,3). The problem is i dont know to split the data set with respective dummy variables as to create box plots and ultimately a linear model.
PLease help!! :'(
So I think you can specify which feature is categorical.
Say
data<- read.csv(filename)
data$feature <- factor(data$feature)
Where feature is the feature you want to convert to categorical data?
Is that what you are looking for?
If I get your problem, you have 2 columns, one with factor levels (1, 2, 3) in your example, and another response variable. Is there it? (An example with part of your data would be very helpful). In any case, if your data has this structure you don't need to split it. For a boxplot just run
boxplot(data$variable~data$factor)
You can use the same approach for a linear model:
lm(data$variable~data$factor)
If your data has other structure, you will need to explain it before someone can give further help...