Display a long message in desired format using AngularJS
Date : March 29 2020, 07:55 AM
will be helpful for those in need I have long message string. Each parent message is separated by a symbol # and child message separated by a symbol ^. , You almost done it, check out some modifications: angular.module('app', []).controller('MyCtrl', function($scope) {
$scope.messages = "Message1#Message2#Message3#Message4^Message41^Message42#Message5^Message51^Message52^Message53^Message54^Message55";
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js">
</script>
<ul ng-app='app' ng-controller="MyCtrl">
<li ng-repeat="message in messages.split('#')">
{{message.split('^')[0]}}
<ul>
<li ng-repeat="msg in message.split('^').splice(1)">
{{msg}}
</li>
</ul>
</li>
</ul>
|
For data frames already in 'long' format, how can one make a measured variable an ID variable without transposing the wh
Date : March 29 2020, 07:55 AM
should help you out I am making calls to a database using a package (brapi) that return the necessary data in long format. The api treats certain ID variables as measured variables which is problematic because my analysis scripts need them as ID variables. I can't modify the call to the database in such a way that it will do it for me and I do not want to just reshape2::cast and then melt the entire thing because there are hundreds of variables which would make it expensive. Ideally, I am looking for a way to do this with reshape2, plyr, or the like. , I'll replicate the problem using your approach of using mtcars: df <- head(mtcars) # get a small chunk to work with
df <- df %>%
mutate(id = paste0("id", row_number())) # create an id variable
# use tidyr::gather() to "melt" the data:
df_long <- df %>%
gather()
# Here's the problem area. Focus in on the last '10':
df_long %>% tail(10)
key value
63 carb 1
64 carb 1
65 carb 2
66 carb 1
67 id id1
68 id id2
69 id id3
70 id id4
71 id id5
72 id id6
# get the id strings, store in `ids`
ids <- df_long %>%
filter(key == "id") %>%
select(value) %>%
.[[1]]
df_long <- df_long %>%
mutate(newid = rep(ids, length(unique(.$key)))) %>%
filter(key != "id") %>%
select(newid, key, value)
head(df_long)
newid key value
1 id1 mpg 21
2 id2 mpg 21
3 id3 mpg 22.8
4 id4 mpg 21.4
5 id5 mpg 18.7
6 id6 mpg 18.1
|
Reshape long format to wide format with two columns for each observation in long format
Tag : pandas , By : platformNomad
Date : March 29 2020, 07:55 AM
hope this fix your issue I am trying to reshape a pandas dataframe with the following long format: , First check columns names: print (smallstack.columns.tolist())
['ISO3', 'Indicator', 'Year', 'Value']
df = smallstack.set_index(['ISO3', 'Indicator', 'Year'])['Value'].unstack([1,2])
print (df)
Indicator Pop. density Pupil-teacher ratio
Year 2003 2004 2005 2003 2004 2005
ISO3
FRA 113,6 114,5 115,4 18,6 18,6 18,6
USA 31,7 32,0 32,3 14,8 14,2 14,1
smallstack['Value'] = smallstack['Value'].str.replace(',','.').astype(float)
smallstack.pivot_table(index='ISO3', columns=['Indicator', 'Year'], values='Value')
|
How to convert long format to wide format data over multiple variable (Column) and be stacked onto each other?
Date : March 29 2020, 07:55 AM
this one helps. I have a monthly time series data (1987-2017) for 20 station. I want to convert the long format data to wide format data such that all the data covering 20 station are in one data frame. , A solution using data.table DT <- as.data.table(df)
#filling all ne NAs for optical rasons
DT[, c("stn1", "stn2") := .(sample(1:100, 30), sample(1:100, 30))]
dcast(DT, Month ~ Year, value.var = c("stn1", "stn2")) %>%
melt(id.vars = 1, variable.name = "year") %>%
dcast(year ~ Month, value.var = "value") -> DT2
year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1: stn1_1987 98 5 89 84 75 36 4 34 26 78 33 28
2: stn1_1988 67 74 40 63 9 19 79 61 66 93 47 62
3: stn1_1989 68 29 7 46 54 87 NA NA NA NA NA NA
4: stn2_1987 31 61 74 89 46 54 70 80 84 6 96 32
5: stn2_1988 75 71 11 99 20 7 77 13 52 14 2 41
6: stn2_1989 83 22 97 43 59 15 NA NA NA NA NA NA
DT2[, c("stn", "year") := tstrsplit(year, "_", fixed=TRUE)]
year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec stn
1: 1987 98 5 89 84 75 36 4 34 26 78 33 28 stn1
2: 1988 67 74 40 63 9 19 79 61 66 93 47 62 stn1
3: 1989 68 29 7 46 54 87 NA NA NA NA NA NA stn1
4: 1987 31 61 74 89 46 54 70 80 84 6 96 32 stn2
5: 1988 75 71 11 99 20 7 77 13 52 14 2 41 stn2
6: 1989 83 22 97 43 59 15 NA NA NA NA NA NA stn2
|
Change format of long variable?
Tag : java , By : user186012
Date : March 29 2020, 07:55 AM
|