I'm beginning for r programming language, help me how to add list of value in data frame columns.
my expected data frame will be.
U_ID Value
1 list(`First`="ty",'Second'="89")
2 list(`First`= c("20","10","40"),`Second`="user")
3 list(`First`="vendor",`Second`="yu",`Four`=list(list(`ty`="78",'pt'="kkkpp")))
4 NULL
5 list(`First`="client")
Related
I want to replace values in a column using R code. Data frame is DT. The column name is Data. It has values 1 and 2 repeatedly. I want a code to replace all 1 with Asia and 2 with India.
This question already has answers here:
How to subset matrix to one column, maintain matrix data type, maintain row/column names?
(1 answer)
How do I extract a single column from a data.frame as a data.frame?
(3 answers)
Closed 5 years ago.
I am running a simulation model that creates a large data frame as its output, with each column corresponding to the time-series of a particular variable:
data5<-as.data.frame(simulation3$baseline)
Occasionally I want to look at subsets, especially particular columns, of this data frame in order to get an idea of the output. For this I am using the View-function like so
View(data5[1:100,1])
for instance, if I wish to see the first 100 rows of column 1. Alternatively, I also sometimes do something like this, using the names of the time series:
timeframe=1:100
toAnalyse=c("u","u_n","u_e","u_nw")
View(data5[timeframe,toAnalyse])
In either case, there is an annoying display problem when I am trying to view a single column on its own (as for instance with View(data5[1:100,1])), whereby what I get looks like this:
Example 1
As you can see, the top of the table which would usually contain the name of the variable in the dataset instead contains a string of all values that the variable takes. This problem does not appear if I select 2 or more columns:
Example 2
Does anyone know how to get rid of this issue? Is there some argument that I can feed to View to make sure that it behaves nicely when I ask it to just show a single column?
View(data5[1:100,1, drop=FALSE])
When you access a single column of a data frame it is converted to a vector, drop=FALSE prevents that and retains the column name.
For instance:
> df
n s b
1 2 aa TRUE
2 3 bb TRUE
3 5 cc TRUE
> df[, 1]
[1] 2 3 5
> df[, 1, drop=FALSE]
n
1 2
2 3
3 5
I have two data frames with totally different column names and values.
Example :
Data Frame 1 ->
company value
A 10
B 11
A 9
Data Frame 2 ->
id value2
Q 7
W 8
E 9
This question has several parts that I want to achieve:
Extract the unique values of COMPANY column from
data frame 1 based on the COMPANY column(Unique companies)
Copy the unique values obtained above into a NEW
COLUMN in Data Frame 2 RANDOMLY (only company field)
Merge the two data frames based on the unique value
column.(This is only for testing, hence why I need this step)
All help is appreciated!!
Thank you in advance.
You could try something like this:
company <- unique(df1$company)
df2$new_column <- sample(company, nrow(df2), replace = TRUE)
So I get that the title is terrible and generic like. I have no idea how to concisely describe what I am trying to do.
I've got a 2 column data frame in R, column A has data values, column B had data that has now been binned (was year associated with Column A, now is a bin label based on year ranges).
I need to generate a new data frame which uses the bin labels as columns with the associated data values as row entries, preferably sorted, back-filled with 'NA' to prevent columns of different lengths.
Sample data:
df <- data.frame(values=c(1,NA,3,NA,5:6,7:9),
bins=rep(c("yr1_yr2","yr2_yr3","yr3_yr4"),each=3))
SOLUTION EDIT: So after a lot of experimentation I was able to do what I wanted with my data by using the 'cut_width' function from ggplot2 to slice my data into bins then plop it in a distribution graph.
Thank you all for your attempts, sorry again for the vague question and lack of sample data.
Not quite sure if this is getting close to what you want...
library(tidyverse)
reshape2::melt(df, id.vars='bins', measure.vars='values')
returns
bins variable value
1 yr1_yr2 values 1
2 yr1_yr2 values NA
3 yr1_yr2 values 3
4 yr2_yr3 values NA
5 yr2_yr3 values 5
6 yr2_yr3 values 6
7 yr3_yr4 values 7
8 yr3_yr4 values 8
9 yr3_yr4 values 9
I'm trying to figure out how can I add something to a data frame df, based on a variable (i.e. a date), ending up with a data frame named df_17 if variable is equal to 2017 for example.
The reason why I want this is because I'm importing datasets from several years and quarters, and I would like to make sure that they are named according to the year variable they have. Each dataset only has 1 date. I know I can do it manually but it would take me less time to automate it.
I know how to do it with columns and rows, but I can't figure it out for objects.
EDIT:
Example 1:
Data frame name "df"
A B Date
1 4 2017
2 3 2017
New data frame name "df_2017"
Example 2:
Data frame name "df"
A B Date
1 4 2016
2 3 2016
New data frame name - "df_2016 "
The assign function should do what you want. A solution could look like
assign(paste0("df_", year), dataframe_read_from_file, pos = 1)
If you use assign inside a function oder a loop, make sure that you set the pos option correctly.